题解 | #尼科彻斯定理#

尼科彻斯定理

https://www.nowcoder.com/practice/dbace3a5b3c4480e86ee3277f3fe1e85

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});
rl.on("line", function (line) {
    const n = Number(line);
    const ans = [];
    let item = -1; // 遍历到的奇数 从-1开始  是为了第1行
    // 可以理解为  第n行有n个奇数相加,第1行1个奇数、第2行两个奇数、第3行3个奇数......
    // 而取哪个奇数呢?则是从上一行的最后一个奇数+2作为当前行第一个
    for (let i = 1; i <= n; i++) {
        let j = 0;
        while (j < i) {
            item += 2;
            j++;
            if (i == n) {
                ans.push(item)
            }
        }
    }
    console.log(ans.join('+'))
});

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务