题解(英文注释)
2023-07-17 13:15:58
发布于:上海
#include <bits/stdc++.h>
using namespace std;
int matrix[45][45];
int main(){
//initializing matrix
int user;
cin>>user;
matrix[0][(user-1)/2]=1;
int lastplace[2] = {0,(user-1)/2};//list for storing coord values
for (int i=2;i<=user*user;i++){
if (lastplace[0]==0){
if (lastplace[1]==user-1){//if is element at top right
matrix[1][user-1]=i;
lastplace[0]=1;
lastplace[1]=user-1;
} else {//if is element in first line but not top right
matrix[user-1][lastplace[1]+1]=i;
lastplace[0]=user-1;
lastplace[1];
}
}else if (lastplace[1]==user-1){//if element is in last row but not in first line
matrix[lastplace[0]-1][0]=i;
lastplace[0]--;
lastplace[1]=0;
} else {
if (matrix[lastplace[0]-1][lastplace[1]+1]==0){//if top is empty then insert element
matrix[lastplace[0]-1][lastplace[1]+1]=i;
lastplace[0]--;
lastplace[1];
}
else {//else we insert element under the original coords
matrix[lastplace[0]+1][lastplace[1]]=i;
lastplace[0];
}
}
}
//print the matrix
for (int i=0;i<user;i){
for (int g=0;g<user;g++){
cout<<matrix[i][g]<<" ";
}
cout<<endl;
}
}
全部评论 38
6
2023-07-24 来自 上海
06
2023-07-24 来自 上海
06
2023-07-24 来自 上海
06
2023-07-24 来自 上海
06
2023-07-24 来自 上海
06
2023-07-24 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
06
2023-07-22 来自 上海
0
有帮助,赞一个