模拟
用d来表示偏移量
#include <iostream>
using namespace std;
const int N = 110;
int g[N][N];
int main()
{
int n;
while(cin >> n, n)
{
int d = 0;
while(d < n)
{
for(int i = d, j = 1;i < n;i ++ ,j ++ ) g[d][i] = j, g[i][d] = j;
d ++ ;
}
for(int i =0;i < n;i ++ )
{
for(int j = 0;j < n;j ++ ) cout << g[i][j] << ' ';
cout << endl;
}
cout << endl;
}
return 0;
}
平方矩阵2规律:x = abs(i-j) + 1
#include <iostream>
using namespace std;
int main()
{
int n;
int x;
while(cin >> n ,n)
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
x = abs(i-j) + 1;
cout<<x<<" ";
}
cout<<endl;
}
cout<<endl;
}
}