我试图打印2 ^ n中的数字总和,n = 1到1000.这就是我所做的.
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 + " ");
}
}
Run Code Online (Sandbox Code Playgroud)
它只能工作到大约2 ^ 30左右,然后打印相同的结果,46,其余的.
我在C中使用"long long"尝试过类似的东西,并且在类似限制之后打印0.
根据答案,我改变了
BigInteger power = BigInteger.valueOf((int)Math.pow(2, i));
Run Code Online (Sandbox Code Playgroud)
至
BigInteger power = BigInteger.valueOf(2).pow(i);
Run Code Online (Sandbox Code Playgroud)
和46改为0.就像C.仍然没有工作......
| 归档时间: |
|
| 查看次数: |
245 次 |
| 最近记录: |