将矩阵划分为n个矩阵

时间:2017-05-14 18:53:11

标签: java matrix divide

我有一个N×N矩阵(随机生成)。

我需要将其划分为C矩阵(C是用户提供的矩阵数)。

例如,如果我们拥有的4 X 4矩阵是:

0     5     6     9
3     0     1     3
8     1     0     2
9     2     4     0

并且用户输入2作为C,然后结果应为:

Matrix 1
    0     5     6     9
    3     0     1     3

Matrix 2
    8     1     0     2
    9     2     4     0

1 个答案:

答案 0 :(得分:1)

public class TestMain {

    int[][] rr = new int[][]{
        {0, 5, 6, 9},
        {3, 0, 1, 3},
        {8, 1, 0, 2},
        {9, 2, 4, 0}};

    public TestMain() {
        getHalfMatrix(rr);
    }

    public void getHalfMatrix(int[][] mrix) {
        int st = (int) mrix.length / 2;
        System.out.print("Matrix1\n");
        for (int i = 0; i < st; i++) {
            for (int j = 0; j < mrix[0].length; j++) {
                System.out.print("\t" + mrix[i][j]);
            }
            System.out.print("\n");
        }
        System.out.print("Matrix2\n");
        for (int i = st; i < mrix.length; i++) {
            for (int j = 0; j < mrix[0].length; j++) {
                System.out.print("\t" + mrix[i][j]);
            }
            System.out.print("\n");
        }
    }

    public static void main(String[] args) {
        new TestMain();

    }

}

OUTPUT是:

Matrix1
    0   5   6   9
    3   0   1   3
Matrix2
    8   1   0   2
    9   2   4   0