这是一道3进制的问题。
我想遍历从0到2222222222,逢3进1如从0到22所有的数字为:
00
10
20
01
11
21
02
12
22本人算法如下:
public class Test { public static void main(String[] args) {
String str="00";
char[] ch=str.toCharArray();
int i=0;
while (!str.equals("22")) {
method(ch,i);
str=new String(ch);
System.out.println(str);
} }

private static void method(char[] c,int j){
int tem=Integer.parseInt(""+c[j])+1;
if(tem==3){
tem=0;
method(c,j+1);
}
c[j]=(""+tem).charAt(0);
tem=0;
}
}但上面的算法处理少位还可以,若处理10位以上数字的话超慢,希望各位大虾指点一下,有没有更快的算法(若有好的算法,一定给分,谢谢了)

解决方案 »

  1.   

    public class test1 { public static void main(String[] args) { for (int i=0;i<177147;i++){
    pout(i);
    }
    } private static void pout( int num ){
    int j;
    String str = "";
    while (num>0){
    j = num % 3;
    num = num / 3;
    str = j + str;
    }
    System.out.println(str);
    }
    }
      

  2.   

    for(int i=0;i<=Integer.parseInt("2222222222",3);i++){
                System.out.println(Integer.toString(i,3));
            }
      

  3.   


    test1:10位数,用时:141毫秒test2:10位数,用时:406毫秒
      

  4.   

    test3:10位数,用时:31毫秒treeroot 利害.