题解 | #杨辉三角的变形#
杨辉三角的变形
https://www.nowcoder.com/practice/8ef655edf42d4e08b44be4d777edbf43
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
// 方法一:模拟取最后一行遍历,运行超时22/30 组用例通过。用例输入10000
void async function () {
let n = parseInt(await readline());
if(n <=2) return console.log(-1);
let pre = [1],cur = [];
while(--n){
pre.unshift(0);
pre.unshift(0);
for(let i = 0; i < pre.length; i++){
cur.push(pre.slice(i,i+3).reduce((a,b)=>a+b,0));
}
[pre,cur]=[[...cur],[]];
}
for(let i = 0; i < pre.length; i++){
if(pre[i]%2===0){
console.log(i+1);
break;
}
}
}()
// 方法二:找规律
void async function () {
const n = parseInt(await readline());
if(n <=2) return console.log(-1);
const rule = [2,3,2,4];
console.log(rule[(n-2+3)%4])
}()
