[英]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.