一般对于数字长度的判断,我们习惯上是采用模除的方法。
 使用递归的思想得到结果。大智慧经典版6.0下载 
 以下是jdk源码中实现的思路:
 -----------------------------------------------------------------------------------------
 final static int[] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
            99999999, 999999999, Integer.MAX_VALUE };
 static int stringSize(int x) {
        for (int i=0; ; i++)
            if (x <= sizeTable[i])
                return i+1;
    }
 -----------------------------------------------------------------------------------------
 递归实现的代码:
 int num = random.nextInt();
            while (num / 10 > 0) {
                num = num / 10;
                length++;
            }
 ------------------------------------------------
 当循环增加到百万数量级时,
 前者的时间差不多是后者数量级的一半 。
posted on 2011-05-17 11:55 
墙头草 阅读(306) 
评论(0)  编辑  收藏