AcWing 754. 平方矩阵 II —— C++
原题链接
简单
作者:
谢同学
,
2024-02-27 21:14:49
,
所有人可见
,
阅读 20
//法一:数组法
#include<iostream>
using namespace std;
int main()
{
int n;
while(cin >> n,n){
int m[110][110] = {0};
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
if(j == 0) m[i][j] = i + 1;
else if(i == 0) m[i][j] = j + 1;
else m[i][j] = m[i - 1][j - 1];
}
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j) cout << m[i][j] << ' ';
cout << endl;
}
cout << endl;
}
return 0;
}
//法二:观察法
#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(i - j) + 1 << ' ';
}
cout << endl;
}
cout << endl;
}
return 0;
}