JavaScript Algorithm Question: Square Matrix
Input an integer N, and output a two-dimensional array of order N.
The outermost layer of the array is 1, the next outermost layer is 2, and so on.
input format
The input consists of multiple lines, each line containing an integer N.
When the input line N=0, it means that the input is over, and this line does not need any processing.
output format
For each input integer N, output a two-dimensional array of order N that meets the requirements.
Each array occupies N lines, and each line contains N integers separated by spaces.
After each array is output, output a blank line.
data range
0≤N≤100
Input sample:
1
2
3
4
5
0
Sample output:
1
1 1
1 1
1 1 1
1 2 1
1 1 1
1 1 1 1
1 2 2 1
1 2 2 1
1 1 1 1
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
let buf = "";
process.stdin.on("readable", function() {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
process.stdin.on("end", function() {
let ns = buf.split('\n').map(x => {
return parseInt(x)});
for (let n of ns) {
if (n === 0) break;
for (let i = 0; i < n; i ++ ) {
let line = "";
for (let j = 0; j < n; j ++ ) {
line += `${
Math.min(i + 1, n - i, j + 1, n - j)} `;
}
console.log(line);
}
console.log();
}
});