java
查看原帖
java
526386
7teen楼主2024/3/13 14:18
import java.util.Scanner;

public class Main7 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[][] a = new int[n][n];
        int x = 0;
        int y = n / 2;

        for (int num = 1; num <= n * n; num++) {
            a[x][y] = num;
            int nextRow = (x - 1 + n) % n;
            int nextCol = (y + 1) % n;
            if (a[nextRow][nextCol] == 0) {
                x = nextRow;
                y = nextCol;
            } else {
                x = (x + 1) % n;
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}
2024/3/13 14:18
加载中...