現有:
AB
C
DE
F
如獲得ACDF ACEF BCDE BCEF的 java算法
public class TestMain {
private static int size;
public static void main(String args[]) throws Exception {
char [][] data = new char[][]{
{'A', 'B'},
{'C'},
{'D', 'E'},
{'F'}
};
int length = 1;
for (int i=0; i<data.length; i++) {
length *= data[i].length;
}
String[] result = new String[length];
setString(data, 0, "", result);
for (int i=0; i<length; i++) {
System.out.println(result[i]);
}
}
private static void setString(char[][] data, int curIndex, String string, String[] result) {
if (data.length == curIndex) {
result[size++] = string;
} else {
for (int j=0; j<data[curIndex].length; j++) {
setString(data, curIndex+1, string + data[curIndex][j], result);
}
}
}
}