求一个关于矩阵取数的算法,最好能够写成java程序

1个回答

  • public class FindGroup {

    public static void main(String[] args) {

    printGroup(3, 4, 3);

    }

    public static void printGroup(int w, int h, int n) {

    for (int x = 1; x <= w - n + 1; x++) {

    for (int y = 1; y <= h - n + 1; y++) {

    printGroup(valueOf(x, y), x + 1, w, y, h, n - 1);

    }

    }

    }

    private static void printGroup(String prifix, int w1, int w2,

    int h1, int h2, int n) {

    if (n == 0) {

    System.out.println(prifix);

    return;

    }

    for (int x = w1; x <= w2 - n + 1; x++) {

    for (int y = h1; y <= h2 - n + 1; y++) {

    printGroup(prifix + ", " + valueOf(x, y),

    x + 1, w2, h1, h2, n - 1);

    }

    }

    }

    private static String valueOf(int x, int y) {

    return "x" + x + y;

    }

    }下面为输出:x11, x21, x31

    x11, x21, x32

    x11, x21, x33

    x11, x21, x34

    x11, x22, x31

    x11, x22, x32

    x11, x22, x33

    x11, x22, x34

    x11, x23, x31

    x11, x23, x32

    x11, x23, x33

    x11, x23, x34

    x12, x22, x32

    x12, x22, x33

    x12, x22, x34

    x12, x23, x32

    x12, x23, x33

    x12, x23, x34printGroup(int w, int h, int n)

    函数说明:w为列,h为行,n为题目中的X

    可能不太满足题意,请明确说明以下问题.

    1.第一个数代表列,还是第二个数代表列?2.排列组合有顺序吗?3.必须前面的列在前面吗?例如必须先取第一列,再取第二列或第三列