博客
关于我
codewars练习(javascript)-2021/5/6&7&8&9&10
阅读量:740 次
发布时间:2019-03-22

本文共 1153 字,大约阅读时间需要 3 分钟。

codewars-js练习

2021/5/6

【1】<7kyu>【Greatest common divisor】

求两个正整数的最大公约数

整数可以很大,所以你需要找到一个聪明的解决方案。

思路

一开始采用了简单粗暴的方式(代码如下),然后时间超时。

function mygcd(x, y) {    var i = Math.min(x, y);    var temp = [];    if (x == 1 && y == 1) return 1;    while (i >= 1) {        if (x % i == 0 && y % i == 0) return i;        i--;    }}
解决方法

后来采用了下面的辗转相除法,就通过了。

##### 原理如果有两个数a和b,b为0,那么a和b的最大公约数一定是a。那么就想尽办法让它出现0的情况。### 2021/5/7【1】<7kyu>【Sum of Triangular Numbers】#### 返回包括第n个三角形数在内的所有三角形数的和。##### 示例[01] 02 [03] 04 05 [06] 07 08 09 [10] 11 12 13 14 [15] 16 17 18 19 20 [21] 如果输入4,则返回1 + 3 + 6 + 10 = 20。##### 解决方法```javascript

2021/5/8

【1】<7kyu>【Invisible cubes】

问题描述

给定的n对应的不可见立方体数。

示例

输入:0输出:0输入:2输出:27

解决方法

【2】<6kyu>【Arabian String】

创建一个可以将字符串从不同格式转换为PascalCase的方法。这也必须支持符号。

示例

"example name" → "ExampleName""your-NaMe-here" → "YourNameHere""testing ABC" → "TestingAbc"

解决方法

2021/5/9

【1】<8kyu>【What’s the real floor?】

写一个函数,在美国系统中给定的楼层返回在欧洲系统中的楼层。

示例

1 → 02 → 0513 → 41514 → 13-316 → -3

解决方法

2021/5/10

【1】<8kyu>【Multiplication table for number】

返回一个从1到10的整数的乘法表。

示例

1 * 5 = 52 * 5 = 103 * 5 = 154 * 5 = 205 * 5 = 256 * 5 = 307 * 5 = 358 * 5 = 409 * 5 = 4510 * 5 = 50

解决方法

转载地址:http://oxdwk.baihongyu.com/

你可能感兴趣的文章
npm install 卡着不动的解决方法
查看>>
npm install 报错 EEXIST File exists 的解决方法
查看>>
npm install 报错 ERR_SOCKET_TIMEOUT 的解决方法
查看>>
npm install 报错 fatal: unable to connect to github.com 的解决方法
查看>>
npm install 报错 no such file or directory 的解决方法
查看>>
npm install 权限问题
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm install的--save和--save-dev使用说明
查看>>
npm node pm2相关问题
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm scripts 使用指南
查看>>
npm should be run outside of the node repl, in your normal shell
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>
npm 下载依赖慢的解决方案(亲测有效)
查看>>
npm 安装依赖过程中报错:Error: Can‘t find Python executable “python“, you can set the PYTHON env variable
查看>>