#28490: CPP


11030067@mail.hpsh.tp.edu.tw (和平110級鄧雨珊)

學校 : 臺北市立和平高級中學
編號 : 163096
來源 : [61.64.210.174]
最後登入時間 :
2022-10-23 16:54:59
a417. 螺旋矩陣 | From: [219.85.157.240] | 發表日期 : 2021-12-13 23:05

#include <iostream>
#include <iomanip>
using namespace std;

int main() {
int T, M, N;
int R[100][100];
cin >> T;
while (T--){
int n=1;
cin >> N >> M;
for (int i=0; i<(N+1)/2; i++){
for (int j=0; j<N-2*i-1; j++){
R[i][i+j]=n++;
}
for (int j=0; j<N-2*i-1; j++){
R[i+j][N-i-1]=n++;
}
for (int j=0; j<N-2*i-1; j++){
R[N-i-1][N-j-i-1]=n++;
}
for (int j=0; j<N-2*i-1; j++){
R[N-i-j-1][i]=n++;
}
}
if (N%2){
R[N/2][N/2]=n;
}
for (int i=0; i<N; i++){
for (int j=0; j<N; j++){
if (M==1){
cout << setw(5) << R[i][j];
}
else {
cout << setw(5) << R[j][i];
}
}
cout <<endl;
}
cout <<endl;;
}
}
 
ZeroJudge Forum