import java.util.Scanner;
//求正方形的回文 :
//例如:输入 5 则输出:
//1 2 3 4 5
//16 17 18 19 6
//15 24 25 20 7
//14 23 22 21 8
//13 12 11 10 9
public class 正方形的回文 {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int n = Integer.parseInt(input.next());
int[][] arr = new int[n][n];
int x = 0;
int y = 0;
int temp = 1; //从开始到回文结束的值
arr[x][y] = temp; // 初始化第一个元素
while(temp<n*n){
//向右 操作列
// System.out.println(temp);
while(y+1<n && arr[x][y+1]==0){
arr[x][++y] = ++temp;
}
while(x+1<n && arr[x+1][y]==0){
arr[++x][y] = ++temp;
}
while(y-1>=0 && arr[x][y-1]==0){
arr[x][--y] = ++temp;
}
while(x-1>=0 && arr[x-1][y]==0 ){
arr[--x][y] = ++temp;
}
}
for(int i=0;i<arr.length;i++){
for(int j=0;j<arr[i].length;j++){
System.out.print((arr[i][j]<10?" "+arr[i][j]:arr[i][j]) +" ");
}
System.out.println();
}
}
}
正方形的回文
最新推荐文章于 2022-12-22 22:48:53 发布