在循环结果的最后一次迭代中是错误的。我知道减法之前的数字可能比 long 大。这就是为什么我将 power 设置为 long 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;
}