AcWing 754. 平方矩阵 II
题意:
本题要你输出满足题意的n阶矩阵。
思路:
观察很重要。
AC(抽风大佬)
# include <bits/stdc++.h>
using namespace std;
int main(){
int n;
while(cin>>n,n){
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ )cout<<(abs(i-j)+1)<<' ';
cout<<endl;
}
cout<<endl;
}
}
AC
# include <bits/stdc++.h>
using namespace std;
int ans[110][110];
int main(){
ios::sync_with_stdio(0);
int n;
while(cin>>n,n){
for(int tot = 1; tot <= n; tot ++ ){
int i = tot, j = 1;
for(i,j; i<=n; i ++,j++ ){
ans[i][j] = tot;
}
i = 1, j = tot;
for(i,j; j<=n; i++, j++){
ans[i][j] = tot;
}
}
for(int i = 1; i <= n; i ++ ){
for(int j = 1; j <= n; j ++ )cout<<ans[i][j]<<' ';
cout<<endl;
}
cout<<endl;
}
return 0;
}