753. square matrix I
Title Description
Enter an integer N, back to shape the output of a two-dimensional array of order N.
1 is in the outermost array, the outer layer is two times, and so on.
Input Format
Comprising multiple input lines, each comprising an integer N.
When the input behavior of N = 0, it indicates the end of input, and the line without any treatment.
Output Format
For each input integer N, the output of a two-dimensional array to meet the requirements of the N-th order.
Each array occupy N rows, each row containing N integers separated by spaces.
After each array output is completed, it outputs a blank line.
data range
0 ≤ N ≤ 100
Sample input:
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
Difficulty: hard |
---|
Time / space restrictions: 1s / 64MB |
By the total number: 79 |
The total number of attempts: 170 |
Source: grammar questions |
Algorithms Tags: Array |
AC Code
#include <iostream>
#include <cstdio>
using namespace std;
int main(void)
{
int n, arr[105][105] = {0}, i, j;
while(scanf("%d",&n) != EOF){
if(n == 0){
break;
}
for(i = 1; i <= n; i ++){
for(j = i; j <= n - i + 1; j ++){
arr[j][i] = i;
arr[i][j] = i;
arr[j][n - i + 1] = i;
arr[n - i + 1][j] = i;
//二重循环内的内容是为了遍历到外围的内容
//i是输出的内容变量第一层外围都是1.第二层外围都是2,以此类推。
}
}
for(i = 1; i <= n; i ++){
for(j = 1; j <= n; j ++){
cout<<arr[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
return 0;
}