在斜条纹中打印矩阵的元素

时间:2016-04-14 11:29:28

标签: java matrix zigzag

我希望以这种形式生成一个从1开始的连续数字的矩阵 zig zag matrix

public static int[][] Zig_Zag(final int size) {
        int[][] data = new int[size][size];
        int i = 1;
        int j = 1;
        for (int element = 0; element < size * size; element++) {
            data[i - 1][j - 1] = element;
            if ((i + j) % 2 == 0) { // Even stripes if (j < size) j++; else i+=
                                    // 2; if (i > 1) i--; } else { // Odd
                                    // stripes if (i < size) i++; else j+= 2; if
                                    // (j > 1) j--; } } return data; }
            }
        }
        return data;
    }

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

试试这个

public static int[][] Zig_Zag(int size) {
    int[][] a = new int[size][size];
    int n = 1;
    for (int r = size, c = 0; r >= 0; --r)
        for (int i = r, j = c; i < size; ++i, ++j)
            a[i][j] = n++;
    for (int r = 0, c = 1; c < size; ++c)
        for (int i = r, j = c; j < size; ++i, ++j)
            a[i][j] = n++;
    return a;
}

int[][] a = Zig_Zag(4);
for (int[] r : a)
    System.out.println(Arrays.toString(r));

结果:

[7, 11, 14, 16]
[4, 8, 12, 15]
[2, 5, 9, 13]
[1, 3, 6, 10]

答案 1 :(得分:0)

试试这段代码:

from Tkinter import *

输出:

public static int[][] Zig_Zag(final int size) {
        int[][] data = new int[size][size];
        int i = 1;
        int j = 1;
        for (int element = 1; element <= size * size; element++) {
            data[i - 1][j - 1] = element;
            if ((i + j) % 2 == 0) {
                // Even stripes
                if (j < size)
                    j++;
                else
                    i += 2;
                if (i > 1)
                    i--;
            } else {
                // Odd stripes
                if (i < size)
                    i++;
                else
                    j += 2;
                if (j > 1)
                    j--;
            }
        }
        return data;
    }

    public static void main(String[] args) {
        int[][] data = Zig_Zag(4);
        for(int i=0; i<data.length;i++){
            for(int j=0; j<data[i].length;j++){
                System.out.print(data[i][j]+" ");
            }
            System.out.println("");
        }
    }

答案 2 :(得分:0)

不是一个非常优雅的解决方案:

private static int triangle_below(int n) {
    return n * (n + 1) / 2;
}

private static int except_triangle_above(int size, int n) {
    return size * size - triangle_below(2 * size - n);
}

private static int[][] gen(int size) {
    int[][] m = new int[size][size];

    for (int i = 0; i < size; ++i) {
        for (int j = 0; j < size; ++j) {
            // already filled cells in lower diagonal layers
            int k = Math.min(
                triangle_below(i + j),
                except_triangle_above(size, Math.max(size, i + j + 1))
            );

            // position in current layer
            int l = Math.min(j + 1, size - i);

            m[size - i - 1][j] = k + l;
        }
    }

    return m;
}