簡體   English   中英

如何用int數組表示50位整數?

[英]How to represent a 50 digit integer with an array of int?

該准則要求以下內容:

BigIntegers will be represented with 50 digit arrays of int (where each integer in the array is an integer in the range 0..9).

You will have a class called BigInteger that has the following methods:
BigInteger( ) --- initialize the BigInteger to 0
BigInteger(int n) --- initialize the BigInteger to the value of n
BigInteger( BigInteger n) --- a copy constructor

我的問題是,最有效的方法是什么? 目前,我有:

public class BigInteger {
    int[] BigInteger = new int[50];

    public BigInteger() {
        for(int i = 0; i < BigInteger.length; i++) {
            BigInteger[i] = 0;
        }
    }

這似乎可行,但僅用於將數組初始化為0。...我檢查了堆棧溢出,但是空了。 有人可以為我指出正確的方向嗎?

我不是Java家伙,但是那又如何呢?

public BigInteger() {
    for(int i = 0; i < BigInteger.length; i++) {
        BigInteger[i] = 0;
    }
}

public BigInteger(BigInteger bigInteger) {
    for(int i = 0; i < BigInteger.length; i++) {
        BigInteger[i] = bigInteger[i];
    }
}

public BigInteger(int n) {
    String nstr = n.toString(); // not sure
    int pos = 49;
    for(int i = nstr.length - 1; i >= 0 ; i--) {
        BigInteger[pos] = Integer.parse(nstr [i]); // parse each char, you get the idea
        pos--;
    }
}

編輯感謝@Andreas。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM