ABCDE 排列组合

来源:互联网 发布:淘宝店铺卖 编辑:程序博客网 时间:2024/04/26 22:03
//permutation and combination
public static void pc() {
String s = "ABCDE";
char[] ch = s.toCharArray();
char[] k = new char[5];
for (int i = 0; i < ch.length; i++) {
k[0] = ch[i];
for (int j = 0; j < ch.length; j++) {
k[1] = ch[j];


for (int q = 0; q < ch.length; q++) {
k[2] = ch[q];


for (int p = 0; p < ch.length; p++) {
k[3] = ch[p];


for (int f = 0; f < ch.length; f++) {
k[4] = ch[f];
String key = new String(k);
if (check(key)) {
System.out.println("--" + key);
}
}
}
}
}
}
}


public static boolean check(String key) {
int total = 0;
for (int i = 0; i < 5; i++) {
total += format(key.charAt(i) + "");
}
return total == 11111;
}


public static int format(String key) {


switch (key) {
case "A":
return 1;
case "B":
return 10;
case "C":
return 100;
case "D":
return 1000;
case "E":
return 10000;
default:
break;
}


return 0;
}
0 0
原创粉丝点击