剑指offer--面试题12:打印1到最大的n位数--Java实现

题目描述:
输入数字n,按顺序打印出从1到最大的n位十进制数,比如输入3,则打印出1、2、3直到最大的3位数即999。

思路:
首先应该意识到这是一个大数问题
Java中有两个提供高精度计算的类,即BigInteger和BigDemical。BigInteger支持任意精度的整数,而BigDemical支持任意精度的定点数。

如果利用这个类,可以容易的写出以下代码:

public static void printWithBigInteger(int n) {
        if (n <= 0) {
            return;
        }

        BigInteger number = BigInteger.ONE;
        int i = 0;
        while (i++ < n) {
            number = number.multiply(BigInteger.TEN);
        }

        for (BigInteger j = BigInteger.ZERO; j.compareTo(number) < 0; j = j
                .add(BigInteger.ONE)) {
            System.out.println(j);
        }
    }

但是如果要求不用BigInteger呢,就要用字符串或者数组表达大数。下面采用字符数组来存储。

public static void printWithoutBigInteger(int n) {
        if (n <= 0) {
            return;
        }

        char[] number = new char[n];

        // 初始化为0
        for (int i = 0; i < n; i++) {
            number[i] = '0';
        }

        // 一直在number上+1,直到到达最大数
        while (!increment(number)) {
            printNumber(number);
        }
    }

这里有两个需要注意的地方:
while (!increment(number)) {
printNumber(number);
}
1.increment什么时候停止。
最简单的方法就是最高位有进位的时候。
2.printNumber就是简单的讲字符数组打印出来吗?
如果n = 3, 不能打印012,而要将0去掉。
所以下面是两个函数的实现:

/**
     * number++操作并判断是否已经达到最大的数,
     * 
     * @param number
     *            当前数
     * @return 是否还可以继续增加
     */
    private static boolean increment(char[] number) {

        // 是否溢出,表示是否达到最大数
        boolean isOverFlow = false;
        // 进位
        int nTakeOver = 0;

        int nLength = number.length;

        for (int i = nLength - 1; i >= 0; i--) {

            int nSum = number[i] - '0' + nTakeOver;
            if (i == nLength - 1) {
                nSum++;
            }
            if (nSum >= 10) {
                if (i == 0)
                    isOverFlow = true;
                else {
                    nSum -= 10;
                    nTakeOver = 1;
                    number[i] = (char) ('0' + nSum);
                }
            } else {
                number[i] = (char) ('0' + nSum);
                break;
            }

        }

        return isOverFlow;
    }

    /**
     * 打印当前数
     * 
     * @param number
     *            当前数
     */
    private static void printNumber(char[] number) {

        boolean isBegining0 = true;
        int nLength = number.length;

        for (int i = 0; i < nLength; i++) {
            if (isBegining0 && number[i] != '0') {
                isBegining0 = false;
            }

            if (!isBegining0) {
                System.out.print(number[i]);
            }
        }
        System.out.println();
    }

    public static void main(String[] args) {

        printWithoutBigInteger(2);
    }

至于递归方法,后面会在全排列的问题上一同给出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值