Java三阶幻方的8种结果输出

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java三阶幻方的8种结果输出相关的知识,希望对你有一定的参考价值。

package com.imooc.text;
/**
 * 三阶幻方8种结果
 * @author 墨北之南
 *
 */
public class Magic {

    public static void main(String[] args) {

        int a = 0, b = 1;
        square1(a, b);
        System.out.println();
        square2(a, b);
        System.out.println();
        a = 2;
        b = 1;
        square3(a, b);
        System.out.println();
        square4(a, b);
        System.out.println();

    }

    public static void square1(int i, int j) {
        int n = 3, mat[][] = new int[n][n];
        for (int k = 1; k <= n * n; k++) {
            mat[i][j] = k;
            if (k % n == 0)
                i = (i + 1) % n;
            else {
                i = (i - 1 + n) % n;
                j = (j + 1) % n;
            }
        }
        for (i = 0; i < mat.length; i++) {
            for (j = 0; j < mat[i].length; j++)
                System.out.print(mat[i][j] + " ");
            System.out.println();
        }
        System.out.println();
        reverse(mat);

        print1(mat);

    }

    public static void square2(int i, int j) {
        int n = 3, mat[][] = new int[n][n];
        for (int k = 1; k <= n * n; k++) {
            mat[i][j] = k;
            if (k % n == 0)
                i = (i + 1) % n;
            else {
                i = (i - 1 + n) % n;
                j = (j - 1 + n) % n;
            }
        }
        for (i = 0; i < mat.length; i++) {
            for (j = 0; j < mat[i].length; j++)
                System.out.print(mat[i][j] + " ");
            System.out.println();
        }
        System.out.println();
        reverse(mat);
        print1(mat);

    }

    public static void square3(int i, int j) {
        int n = 3, mat[][] = new int[n][n];
        for (int k = 1; k <= n * n; k++) {
            mat[i][j] = k;
            if (k % n == 0)
                i = (i - 1 + n) % n;
            else {
                i = (i + 1 + n) % n;
                j = (j + 1) % n;
            }
        }
        for (i = 0; i < mat.length; i++) {
            for (j = 0; j < mat[i].length; j++)
                System.out.print(mat[i][j] + " ");
            System.out.println();
        }
        System.out.println();
        reverse(mat);

        print1(mat);

    }

    public static void square4(int i, int j) {
        int n = 3, mat[][] = new int[n][n];
        for (int k = 1; k <= n * n; k++) {
            mat[i][j] = k;
            if (k % n == 0)
                i = (i - 1 + n) % n;
            else {
                i = (i + 1 + n) % n;
                j = (j - 1 + n) % n;
            }
        }
        for (i = 0; i < mat.length; i++) {
            for (j = 0; j < mat[i].length; j++)
                System.out.print(mat[i][j] + " ");
            System.out.println();
        }
        System.out.println();
        reverse(mat);
        print1(mat);

    }

    public static void reverse(int temp[][]) {
        for (int i = 0; i < temp.length; i++) {
            for (int j = i; j < temp[i].length; j++) {
                int k = temp[i][j];
                temp[i][j] = temp[j][i];
                temp[j][i] = k;
            }
        }
    }

    public static void print1(int temp[][]) {
        for (int i = 0; i < temp.length; i++) {
            for (int j = 0; j < temp[i].length; j++) {
                System.out.print(temp[i][j] + " ");
            }
            System.out.println();
        }
    }
}

 

以上是关于Java三阶幻方的8种结果输出的主要内容,如果未能解决你的问题,请参考以下文章

Java 实现任意N阶幻方的构造

三阶幻方

topK的3种解法

Hihocoder1662 : 查找三阶幻方([Offer收割]编程练习赛40)(暴力)

蓝桥杯基础题2017年省赛—九宫幻方

LQ0127 九宫幻方枚举