java程序中的组合冗余

时间:2014-10-18 15:26:10

标签: java combinations

我想通过使用没有更改字母顺序的给定字母打印每个可能的组合,所以我写了这段代码,但它会一次又一次地打印每一行是什么问题

public class Solutions {


    public static void main(String[] args) {
        //  Scanner scanner = new Scanner(System.in);

        c = "l u k".split(" ");
        Solutions solutions = new Solutions();
        solutions.combi(0);
        System.out.println("Number of combi = " + count);

        System.out.print(max);
    }

    static String[] c;
    static int count = 0;
    static int max = 0;


    public void combi(int start) {

        int j;
        if (start != 0) {
            String str = "";
            for (int i = 0; i < start; i++) {
               // System.out.print(c[i]);
                str += c[i];
            }


          //  System.out.println();
            count++;
        }

        for (j = start; j < c.length; j++) {


            combi(start + 1);


        }

    }

}

1 个答案:

答案 0 :(得分:0)

public void combi(int start) {
    int j;
    if (start != 0) {
        for (int i = 0; i < start; i++) {
            System.out.print(c[i]);
        }

        System.out.println();
        count++;
    } else {
        for (j = start+1; j <= c.length; j++) {
            combi(j);
        }
    }
}