for 循环中 i 最大值为 100,而数组 factorials 大小就是 100 ,从 0 开始算,最大是 99,找不到factorials[100];
ar_size改为101就好了,原书的程序清单5.4也是,size是16,最后输出15的阶乘。
include
using namespace std;
const int ArSize = 16;
int main() {
long long factorials[ArSize];
factorials[1] = factorials[0] = 1LL;
for (int i = 2; i < ArSize; i++)
{
factorials[i] = i * factorials[i - 1];
}
for (int i = 0; i < ArSize; i++)
{
cout << i << "! = " << factorials[i] << "\n";
}
return 0;
}
for 循环中 i 最大值为 100,而数组 factorials 大小就是 100 ,从 0 开始算,最大是 99,找不到factorials[100]; ar_size改为101就好了,原书的程序清单5.4也是,size是16,最后输出15的阶乘。
include
using namespace std; const int ArSize = 16;
int main() { long long factorials[ArSize]; factorials[1] = factorials[0] = 1LL; for (int i = 2; i < ArSize; i++) { factorials[i] = i * factorials[i - 1]; } for (int i = 0; i < ArSize; i++) { cout << i << "! = " << factorials[i] << "\n"; } return 0; }