题解
2024-03-02 11:56:53
发布于:广东
2阅读
0回复
0点赞
#include <iostream>
#include <algorithm>
using namespace std;
#define SIZE 101
int main(){
int n;
cin>>n;
int a[n+1][n+1];
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++){
a[i][j] = 0;
}
}
a[1][(n+1)/2] = 1;
for(int k = 2;k<=n*n;k++){
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++){
if(a[i][j]==k-1){
if(i==1 && j!=n){
a[n][j+1] = k;
}else if(i!=1 && j==n){
a[i-1][1] = k;
}else if(i==1 && j==n){
a[i+1][j] = k;
}else if(i!=1 && j!=n){
if(a[i-1][j+1]==0){
a[i-1][j+1] = k;
}else{
a[i+1][j] = k;
}
}
}
}
}
}
for(int i = 1;i<=n;i++){
for(int j = 1;j<=n;j++){
cout<<a[i][j]<<" ";
}cout<<endl;
}
return 0;
}
这里空空如也
有帮助,赞一个