import java.util.Scanner;
public class P2615 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N= scanner.nextInt();
int [][] arr = new int [N+1][N+1];
int k = 2 ;
int x = 1 ;
int y = N/2+1 ;
for (int i = 1; i <=N; i++) {
for (int j = 1; j <=N; j++) {
arr[i][j]=0;
}
arr[1][N/2+1]= 1;
}
while (k<=N*N)
{if (x==1&&y!=N) {
arr[N][y+1]= k;
x= N;
y =y+1;
}else if (y==N&&x!=1) {
arr[x-1][1]= k;
x= x-1;
y= 1;
}else if (x==1&&y==N) {
arr[x+1][y]=k;
x= x+1;
y=y;
}else if (x!=1&&y!=N) {
if (arr[x-1][y+1]==0) {
arr[x-1][y+1]=k;
x=x-1;
y=y+1;
}else {
arr[x+1][y]=k;
x=x+1;
y=y;
}
}
k++; }
for (int i = 1; i <=N; i++) {
for (int j = 1; j <=N; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
P2615 [NOIP2015 提高组] 神奇的幻方 java 洛谷
最新推荐文章于 2024-07-13 22:56:10 发布