实现{0,0,0,0,0,0}到{5,5,5,5,5,5}
现在有几个初始值,
数组长度6-----a
元素最大值5-----n
按一定顺序从{0,0,0,0,0,0}遍历到{5,5,5,5,5,5}
000000->000001->000002......->000005->000010......
我本来考虑10进制转化为n进制,但是感觉效率比较低
?
import java.util.Arrays;public class Ergodic {public static void main(String[] args) {int length = 3;//数组长度int max=4;//每一位的最大值int[] a= new int[length];//初始化数组foreach(a,length,max);}private static void foreach(int[] a,int length, int max) {int all =(int) Math.pow(max+1, length); //要遍历的总数字 for(int i=0;i<all;i++){ for(int j=0;j<length;j++){//给数组a的每一位填写对应的数字 int t = (int) Math.pow(max+1, length-j-1);//计算当前位的权值 if(i<t){//如果i小于当前位的权值,该位肯定为0,跳过看下一个 continue; } a[j]=(i/t)%(max+1);//计算当前位填写的数字,最大为max } System.out.print(Arrays.toString(a)+" "); if((i+1)%5==0){ System.out.println(); } }}}[0, 0, 0] [0, 0, 1] [0, 0, 2] [0, 0, 3] [0, 0, 4] [0, 1, 0] [0, 1, 1] [0, 1, 2] [0, 1, 3] [0, 1, 4] [0, 2, 0] [0, 2, 1] [0, 2, 2] [0, 2, 3] [0, 2, 4] [0, 3, 0] [0, 3, 1] [0, 3, 2] [0, 3, 3] [0, 3, 4] [0, 4, 0] [0, 4, 1] [0, 4, 2] [0, 4, 3] [0, 4, 4] [1, 0, 0] [1, 0, 1] [1, 0, 2] [1, 0, 3] [1, 0, 4] [1, 1, 0] [1, 1, 1] [1, 1, 2] [1, 1, 3] [1, 1, 4] [1, 2, 0] [1, 2, 1] [1, 2, 2] [1, 2, 3] [1, 2, 4] [1, 3, 0] [1, 3, 1] [1, 3, 2] [1, 3, 3] [1, 3, 4] [1, 4, 0] [1, 4, 1] [1, 4, 2] [1, 4, 3] [1, 4, 4] [2, 0, 0] [2, 0, 1] [2, 0, 2] [2, 0, 3] [2, 0, 4] [2, 1, 0] [2, 1, 1] [2, 1, 2] [2, 1, 3] [2, 1, 4] [2, 2, 0] [2, 2, 1] [2, 2, 2] [2, 2, 3] [2, 2, 4] [2, 3, 0] [2, 3, 1] [2, 3, 2] [2, 3, 3] [2, 3, 4] [2, 4, 0] [2, 4, 1] [2, 4, 2] [2, 4, 3] [2, 4, 4] [3, 0, 0] [3, 0, 1] [3, 0, 2] [3, 0, 3] [3, 0, 4] [3, 1, 0] [3, 1, 1] [3, 1, 2] [3, 1, 3] [3, 1, 4] [3, 2, 0] [3, 2, 1] [3, 2, 2] [3, 2, 3] [3, 2, 4] [3, 3, 0] [3, 3, 1] [3, 3, 2] [3, 3, 3] [3, 3, 4] [3, 4, 0] [3, 4, 1] [3, 4, 2] [3, 4, 3] [3, 4, 4] [4, 0, 0] [4, 0, 1] [4, 0, 2] [4, 0, 3] [4, 0, 4] [4, 1, 0] [4, 1, 1] [4, 1, 2] [4, 1, 3] [4, 1, 4] [4, 2, 0] [4, 2, 1] [4, 2, 2] [4, 2, 3] [4, 2, 4] [4, 3, 0] [4, 3, 1] [4, 3, 2] [4, 3, 3] [4, 3, 4] [4, 4, 0] [4, 4, 1] [4, 4, 2] [4, 4, 3] [4, 4, 4]import java.util.Arrays;public class Test {public static void main(String[] args) {for(int i=0;i<Math.pow(6, 3);i++) {System.out.println(Arrays.toString(Integer.toString(i,6).toCharArray()));}}}
[0, 0, 0][0, 0, 1][0, 0, 2][0, 0, 3][0, 0, 4][0, 1, 0][0, 1, 1][0, 1, 2][0, 1, 3][0, 1, 4][0, 2, 0][0, 2, 1][0, 2, 2][0, 2, 3][0, 2, 4][0, 3, 0][0, 3, 1][0, 3, 2][0, 3, 3][0, 3, 4][0, 4, 0][0, 4, 1][0, 4, 2][0, 4, 3][0, 4, 4][1, 0, 0][1, 0, 1][1, 0, 2][1, 0, 3][1, 0, 4][1, 1, 0][1, 1, 1][1, 1, 2][1, 1, 3][1, 1, 4][1, 2, 0][1, 2, 1][1, 2, 2][1, 2, 3][1, 2, 4][1, 3, 0][1, 3, 1][1, 3, 2][1, 3, 3][1, 3, 4][1, 4, 0][1, 4, 1][1, 4, 2][1, 4, 3][1, 4, 4][2, 0, 0][2, 0, 1][2, 0, 2][2, 0, 3][2, 0, 4][2, 1, 0][2, 1, 1][2, 1, 2][2, 1, 3][2, 1, 4][2, 2, 0][2, 2, 1][2, 2, 2][2, 2, 3][2, 2, 4][2, 3, 0][2, 3, 1][2, 3, 2][2, 3, 3][2, 3, 4][2, 4, 0][2, 4, 1][2, 4, 2][2, 4, 3][2, 4, 4][3, 0, 0][3, 0, 1][3, 0, 2][3, 0, 3][3, 0, 4][3, 1, 0][3, 1, 1][3, 1, 2][3, 1, 3][3, 1, 4][3, 2, 0][3, 2, 1][3, 2, 2][3, 2, 3][3, 2, 4][3, 3, 0][3, 3, 1][3, 3, 2][3, 3, 3][3, 3, 4][3, 4, 0][3, 4, 1][3, 4, 2][3, 4, 3][3, 4, 4][4, 0, 0][4, 0, 1][4, 0, 2][4, 0, 3][4, 0, 4][4, 1, 0][4, 1, 1][4, 1, 2][4, 1, 3][4, 1, 4][4, 2, 0][4, 2, 1][4, 2, 2][4, 2, 3][4, 2, 4][4, 3, 0][4, 3, 1][4, 3, 2][4, 3, 3][4, 3, 4][4, 4, 0][4, 4, 1][4, 4, 2][4, 4, 3][4, 4, 4]