题解 | #尼科彻斯定理#
尼科彻斯定理
https://www.nowcoder.com/practice/dbace3a5b3c4480e86ee3277f3fe1e85
老规矩,先分享闭眼算法,再分享巧妙算法;
//闭眼算法 while (line = readline()) { //定义n为输出的奇数个数; let n = parseInt(line); //定义m为奇数之和,亦为n的立方; let m = n**3; let start = 0; for (let i = 1; i < m; i+=2) { //定义n个奇数的平均数,起始数为i; let avg = 1/2*(i + i+2*(n-1)); //如果符合n个奇数之和等于m,则找到起始数; if (avg*n == m) { start = i; } } let res = []; //分别push出n个奇数; for (let i = 0; i < n; i++) { res.push(start+ 2*i); } //输出奇数并用"+"连接; console.log(res.join('+')); } //巧妙算法 /* while(line = readline()) { //意外发现起始数等于输入的数字line*(line-1) +1 let start = line * (line - 1) + 1 let res = [] //找到起始数,后面就顺理成章可以for循环出来了 for(let i = 0; i < line; i++) { res.push(start + i * 2) } console.log(res.join('+')) } */