一步到位
#include <iostream>
#include <algorithm>
using namespace std;
int n;
int main()
{
while (cin >> n)
{
for (int i = 0; i < n; i ++ )
{
for(int j = 0; j < n; j ++ )
cout << abs(i - j) + 1 << ' ';
cout << endl;
}
if (n) cout << endl;
}
return 0;
}
按照行和列枚举
#include <iostream>
#include <cstring>
using namespace std;
int n;
int w[110][110];
int main()
{
while(cin>>n,n)
{
memset(w,0,sizeof(w));
for(int i = 1;i <= n;i++)
{
for(int j=i,k=1;j<=n;j++,k++)
{
w[i][j]=k;
w[j][i]=k;
}
}
for(int i = 1;i <= n;i++)
{
for(int j = 1;j <= n;j++)
{
cout<<w[i][j]<<' ';
}
cout<<" "<<endl;
}
cout<<' '<<endl;
}
return 0;
}