import java.math.*;
public class PowerDigitSum {
public static void main(String[] args) {
double[] digits ;
digits = new double[302];
double i = Math.pow(2, 1000);
double c = 301;
double c1 = 0;
double d = 0;
while(c>=0) {
c1 = Math.pow(10, c);
d = Math.floor(i/c1);
i = i - d*c1;
digits[(int)c] = (int)d;
c = c-1;
}
double sum = 0;
c = 0;
while (c<302) {
sum = sum+digits[(int)c];
c= c+1;
}
System.out.println(sum);
}
}输出是1281,但根据projecteuler的说法,这是不正确的。我做错了什么?
发布于 2019-11-07 17:37:30
由于双精度值的性质有限,您无法对其进行正确的数学运算。
您可能可以使用BigDecimal修复代码,但使用BigInteger要容易得多。
https://stackoverflow.com/questions/58745653
复制相似问题