AcWing 754. 平方矩阵 II-找规律
原题链接
简单
作者:
枫哥
,
2024-10-27 15:39:29
,
所有人可见
,
阅读 1
#include <iostream>
#include <cmath>
using namespace std;
// 找规律
// 规律1:输出的元素为abs(i-j) + 1
int main(){
int N;
while (cin >> N,N){
for (int i = 0;i < N;i++){
for (int j = 0; j < N;j++){
int ABS = abs(i-j);
cout << 1 + ABS << " " ;
}
cout << endl;
}
cout << endl;
}
return 0;
}
//规律2:先输出对角线1,再向下右输出234
int num[100][100]; // 数组太大,写成全局变量,防止写成局部变量栈区溢出
int main(){
int n;
while(cin >> n,n)
{
for (int i =0;i < n;i++){
num[i][i] = 1;
for (int j = i+1,k = 2;j < n;j++,k++) num[i][j] = k; // 向右输出
for (int j = i+1,k = 2;j < n;j++,k++) num[j][i] = k; // 向下输出
}
for (int i = 0;i < n;i++){
for (int j = 0;j < n; j++){
cout << num[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
return 0;
}