题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;
void async function () {
// Write your code here
let n = parseInt(await readline());
let a = []
for(let i=0; i<n; i++){
a[i] = [];
}
// 初始化第一行
a[0][0]=1;
for(k=2;k<=n;k++){
a[0].push(a[0][k-2]+k);
}
// 开始跑剩余行,每一个数字等于它右上角 -1
for(let i=1;i<n;i++){
for(let j=0; j<n-i; j++){ // 每一行有效列数都会变,所以限定条件是j<n-i,不是j<n
a[i][j] = a[i-1][j+1]-1;
}
}
for(let i=0; i<n; i++){
console.log(a[i].join(' '));
}
}()

查看15道真题和解析