我试图在 n = 1 到 1000 的情况下打印 2^n 中的数字总和。这就是我所做的。
public static void main(String[] args) {
int n = 1000;
for (int i = 1; i < n; i++) {
BigInteger power = BigInteger.valueOf((int)Math.pow(2, i));
int sum = 0;
while (power.intValue() > 0) {
sum += power.intValue() % 10;
power = power.divide(BigInteger.valueOf(10));
}
System.out.print(sum + " ");
}
}
它只工作到大约 2^30 左右,然后打印相同的结果,46,其余的。
我在 C 中使用“long long”尝试了类似的事情,并且在类似的限制之后打印了 0。
根据答案,我改变了
BigInteger power = BigInteger.valueOf((int)Math.pow(2, i));
至
BigInteger power = BigInteger.valueOf(2).pow(i);
和 46 更改为 0。就像 C 一样。仍然无法正常工作......