如何在屏幕上打印非常大的数字

时间:2017-07-01 10:29:00

标签: java


在我的采访中,我被要求编写一个程序,在屏幕上打印数字1,2,3,4,5 ......直到99999999999999 .....?(最后打印的数字是数字900万次)
您不得使用Big-Integer或任何其他类似对象 我得到的提示是使用模数并使用字符串,我试着考虑它但是没有想到它。
提前致谢

2 个答案:

答案 0 :(得分:1)

您需要一个数组来存储数字,并对数组执行操作。

这是一个例子

public class BigNumberTest2 {

    public static void main(String[] args) {
        /*Array with the digits of the number. 0th index stores the most significant digit*/
        //int[] num = new int[1000000];

        //Can have a million digits, length is 1 + needed to avoid overflow
        int[] num = {0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

        int base = 10;
        int step = 1;
        String endNum = "100000000000000000000000000000000000000000000000000020";//Can have a million digits

        while(true) {
            //Increment by step
            for(int carry = step, i = num.length - 1; carry != 0 && i >= 0; i--) {
                int newDigit = num[i] + carry;

                num[i] = newDigit % base;
                carry = newDigit / base;
            }

            //Find the position of most significant digit
            int mostSignificantDigitIndex = 0;
            while(num[mostSignificantDigitIndex] ==  0) {/*No need to check if firstNonZero < num.length, as start num >=0 */
                mostSignificantDigitIndex++;
            }

            StringBuilder strNum = new StringBuilder();
            //Concatenate to get actual string
            for(int i = mostSignificantDigitIndex; i < num.length; i++) {
                strNum.append(num[i]);
            }

            System.out.println(strNum);

            //Check if number current number is greater or equal to endNum
            if(strNum.length() > endNum.length() || (strNum.length() == endNum.length() && strNum.toString().compareTo(endNum) >= 0)) {
                break;
            }
        }
    }
}

<强>输出

1000000000000000000000000000000000000000000000000000001
1000000000000000000000000000000000000000000000000000002
1000000000000000000000000000000000000000000000000000003
1000000000000000000000000000000000000000000000000000004
1000000000000000000000000000000000000000000000000000005
1000000000000000000000000000000000000000000000000000006
1000000000000000000000000000000000000000000000000000007
1000000000000000000000000000000000000000000000000000008
1000000000000000000000000000000000000000000000000000009
1000000000000000000000000000000000000000000000000000010
1000000000000000000000000000000000000000000000000000011
1000000000000000000000000000000000000000000000000000012
1000000000000000000000000000000000000000000000000000013
1000000000000000000000000000000000000000000000000000014
1000000000000000000000000000000000000000000000000000015
1000000000000000000000000000000000000000000000000000016
1000000000000000000000000000000000000000000000000000017
1000000000000000000000000000000000000000000000000000018
1000000000000000000000000000000000000000000000000000019
1000000000000000000000000000000000000000000000000000020

答案 1 :(得分:0)

这样的事情:

这是一个PHP,但你可以将它转换为java easy ... 通过字符串递增数字的递归。

function nextNum($num="", $step=1, $end="999999999999999999") {
    $string = "";
    $saving = 0;
     for($i=strlen($num)-1; $i>=0; $i--) {
        $calc = intval(intval($num[$i]) + $saving);
        if ($i==(strlen($num)-1)) $calc = $calc + $step;

        if (strlen($calc)==2) {
            $calc = $calc."";
            $saving = intval($calc[0]);
            $calc = intval($calc[1]);
            }
        else {
            $calc = intval($calc);
            $saving = 0;
            }

        $string = $calc . $string;
        }

    if ($saving!=0) $string = $saving.$string;

    echo $string." ";
    if ($end == $string || strlen($end)<strlen($string)) { return; }
    else return nextNum($string, $step, $end);
}
nextNum("0", 1, "999999999999999999");

我没有测试它......但它应该可以工作..