java实现从M个元素中取N个元素的所有组合(数学中的组合问题)

时间:2021-10-15 09:50:56
package reverse;


public class Cat {
public static void main(String[] args) {

int[] s = {4, 2, 1, 3, 0, 5};

String tmp = "";
for(int i=1;i<s.length-1;i++){
tmp = tmp + s[i];
}

String str = tmp;
int length = 2;
printAll(str, length, "");
System.out.println(ss);
}

private static String ss = "";

private static void printAll(String number, int length, String s2) {

if(s2.length() == length){
ss = ss + s2 + ",";

System.out.println(s2 + "  ");
}else{
for(int i = 0;i < number.length(); i++){
System.out.println(new StringBuffer(number).substring(i+1).toString());
System.out.println(length);
System.out.println(s2 + number.charAt(i));
printAll(new StringBuffer(number).substring(i+1).toString(), length, s2 + number.charAt(i));
}
}

}
}