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();
        }
    }
}

 

转载于:https://www.cnblogs.com/eurus/p/7492598.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值