题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
const rl = require("readline").createInterface({ input: process.stdin }); rl.on("line", (line) => { const n = parseInt(line); const arr = [] let count = 1; for (let i = 0; i < n; i++) { arr[i] = [] let k = i for (let j = 0; j <= i; j++) { arr[k--][j] = count++ } } arr.forEach(e => { console.log(e.join(' ')) }) });
按照1234567升序构造即可,每层的规律为i--,j++