Magical magic square (P2615 Los problem solution to a problem Valley, Java language description)

Questions asked

P2615 topic Link
Here Insert Picture Description
Here Insert Picture Description

analysis

Note that the topic said that the magic square of N must be an odd number (measured can not really be an even number), there is no need to detect.

The actual simulation is a map, mobile requirements specified on the line.
This title, looked scary, but in fact, strictly follow the instructions to write code on the line, do not need any algorithm of their own design.

AC Code (Java description language)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        scanner.close();
        int[][] array = new int[num][num];
        int last_x = 0, last_y = num/2;
        //肯定是奇数
        array[0][last_y] = 1;
        for (int i = 2; i <= num*num; i++) {
            if (last_x == 0 && last_y != num-1) {
                last_x = num-1;
                ++last_y;
            } else if (last_x != 0 && last_y == num-1) {
                --last_x;
                last_y = 0;
            } else if (last_x == 0 && last_y == num-1) {
                ++last_x;
            } else if (array[last_x-1][last_y+1] == 0) {
                --last_x;
                ++last_y;
            } else {
                ++last_x;
            }
            array[last_x][last_y] = i;
        }
        StringBuilder result;
        for (int i = 0; i < num; i++) {
            result = new StringBuilder();
            for (int j = 0; j < num; j++) {
                result.append(array[i][j]).append(" ");
            }
            System.out.println(result.toString().trim());
        }
    }
}
Published 717 original articles · won praise 1513 · Views 690,000 +

Guess you like

Origin blog.csdn.net/weixin_43896318/article/details/104853121