0
在循环结果的最后一次迭代中是错误的。我知道在减法之前数字可以比long长。这就是为什么我把权力设定很久。最后一次迭代的结果应该是17888888888888888889.为什么不是?用长整数长计算错误
const int NR_LEVELS = 18;
unsigned long levels[NR_LEVELS];
unsigned long long power = 10;
for(unsigned int i = 0; i < NR_LEVELS; i++) {
levels[i] = ((i+1)*10*power-(i+2)*power+1)/9;
cout << levels[i] << endl;
power *= 10;
}
levels[17] = 17888888888888888889lu;
for(unsigned int i = 0; i < NR_LEVELS; i++) {
cout << levels[i] << endl;
}
aaaarrrrr ...我一整天都知道,但不知何故,我认为long> int并且不能忘记它:C – 2013-04-06 20:34:46
@ lord.didger:代码中也存在类型问题,但即使使用正确的类型,你将无法获得正确的结果。 – nhahtdh 2013-04-06 20:37:40