实现{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()));}}}
。。时间原因(要下班了)没做pad,如果需要可以扔到int数组里就完了,用jdk的还是很舒服滴/** * @param args */public static void main(String[] args) {//System.out.println(Arrays.toString(Integer.toString(100,10).toCharArray()));traversal(3, 4);}/** * * @param a 长度 * @param n 最大值 */public static void traversal(int a, int n) {int[] data = new int[a];int j;for(j=a-1; j>=0; j--) {data[j] = 0;}while(data[0] <= n) {if(data[a-1] == 0){System.out.println();}System.out.print(Arrays.toString(data)+" ");data[a - 1]++;for(j=a-1; j>0; j--) {if(data[j] > n) {data[j] = 0;data[j-1]++;} else {break;}}}}/** * <p> * 构造一个连续的N进制{@link java.lang.StringBuffer <CODE>StringBuffer</CODE>}数组 * </p> * 如按一定顺序从{0,0,0,0,0,0}遍历到{5,5,5,5,5,5} : * 000000->000001->000002......->000005->000010......->555555 * * @param length * : 数组元素的位数 * @param maxValue * : 每位表示的最大数值 * @author Nightjar * @since 2011-06-04 */public static StringBuffer[] dataSystem(int length, int maxValue) {// listSize 为需构造数组的大小,(maxValue+1)^lengthint listSize = (int) Math.pow(maxValue + 1, length);StringBuffer[] sb = new StringBuffer[listSize];for (int i = 0; i < listSize; i++) {int element = i;sb[i] = new StringBuffer();for (int j = length; j > 0; j--) {int baseNumber = (int) Math.pow(maxValue + 1, j - 1);if (element < baseNumber) {sb[i].append(0);} else {int temp = element / baseNumber;sb[i].append(temp);element -= (temp * baseNumber);}}System.out.println(sb[i]);}return sb;}print "{", join(",\t",reverse @ary), "}\n";
}
base6_print $_ for (0..6**6-1);
public class Test {/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubint length=3;int max=4;int[] arrays=new int[length];transform(arrays,max);}public static void transform(int[] array,int max){int result=0;for(int i=0;i<(Math.pow(max+1, array.length));i++){//math.pow 计算的是总共要加的数result++;System.out.print(Arrays.toString(array)+"\t");transformFromTenToMax(result,array,max+1);if((i+1)%(max+1)==0)System.out.println();}}/* * 将十进制转换成max+1进制 * 除max 倒去余数 */private static void transformFromTenToMax(int src,int[] array,int max){for(int i=array.length-1;i>=0;i--){array[i]=src%max;src/=max;}}}
结果
[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]