日期:2014-05-20 浏览次数:20666 次
int[] a = new int[]{1, 1, 1, 1}; outer: while(true){ System.out.println(Arrays.toString(a)); int j = 3; while((a[j]+=1)==4){ a[j]=1; j--; if (j<0) break outer; } }
------解决方案--------------------
乱写的,没这么简单吧?
for (int n1 = 1; n1 <= 3; n1++)
for (int n2 = 1; n2 <= 3; n2++)
for (int n3 = 1; n3 <= 3; n3++)
for (int n4 = 1; n4 <= 3; n4++) {
System.out.print(n1);
System.out.print(n2);
System.out.print(n3);
System.out.print(n4);
System.out.println();
}
------解决方案--------------------
1.先得到无重复key,1,2,3的key就是int[]keys=new int[]{1,2,3}
2.位数是四位,全排列的数一共有keys.length的位数次方,也就是3*3*3*3个
3.用一个数number表示这个排列顺序,number%keys.length表示个位的值为keys[number%keys.length]
十位为keys[number/keys.length%keys.length]
千位为keys[number/keys.length/keys.length%keys.length]
number的范围为0 到 (3*3*3*3-1)
最终一个for就可以搞定