该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
public static void main(String[] args) {
int times = 0;
String str = "";
Set set = new HashSet();
//用1来代表A;
for (int i = 0; i < 4; i++) {
str += i;//str用来记录循环走到底组成字符串,本来可以不用,可是对于出现多个相同字母的我们是要进行过滤的,比如00123,这个如果不过滤之用循环的话会记录是两次,但是记录下来之后放到set中,set总刚好是不能放入相同的元素的。
if (i == 1)
times++;//如果是1(A)的话把times+1
for (int j = 0; j < 4; j++) {
str += j;
if (j == 1)
times++;
for (int k = 0; k < 4; k++) {
str += k;
if (k == 1)
times++;
for (int p = 0; p < 4; p++) {
str += p;
if (p == 1)
times++;
for (int q = 0; q < 4; q++) {
str += q;
if (q == 1)
times++;
if (times % 2 == 0) {
set.add(str);
System.out.println(str); }
//这一步需要理解一下for循环,一个循环在进行到第二次或后面之后,我们我们不是累加字符串,而是把上一个循环的的str加上q,执行一遍循环我们要恢复原样。
str = str.substring(0, str.length() - 1);
if (q == 1) {
times--;
}
}
str = str.substring(0, str.length() - 1);
if (p == 1) {
times--;
}
}
str = str.substring(0, str.length() - 1);
if (k == 1) {
times--;
}
}
str = str.substring(0, str.length() - 1);
if (j == 1) {
times--;
}
}
str = str.substring(0, str.length() - 1);
if (i == 1) {
times--;
}
} System.out.println(set.size());
}