题目描述
这道题和753.很类似,受753.的启发,可以从“距离”的角度来考虑,发现输出矩阵中元素的值与其到对角线的距离有关,即a[i][j]=abs(i-j)+1
。
C++ 代码
#include<iostream>
using namespace std;
int main()
{
int n;
while( cin >> n && n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
cout << abs(j - i) + 1 << " ";
cout << endl;
}
cout << endl;
}
return 0;
}