题目:
修改以下代码
#include <iostream>
const int ArSize = 16;
int main()
{
long long factorials[ArSize];
factorials[0] = factorials[1] = 1LL;
for (int i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i - 1];
for (int i = 0; i < ArSize; i++)
std::cout << i << "! = " << factorials[i] << std::endl;
return 0;
}
使用array对象(替换掉数组)和long double(替换掉long long)并计算100!的值(原代码求的是0到16阶乘)
源代码:
#include <iostream>
#include <array>
const int ArSize = 100;
int main()
{
using namespace std;
array<long double, ArSize>factorials;
factorials.at(0) = factorials.at(1) = 1L;
for (int i = 2; i < ArSize; i++)
{
factorials.at(i) = i * factorials.at(i - 1);
}
cout << 100 << "! = " << factorials.at(99) << endl;
return 0;
}
演示效果:
如果朋友你感觉文章的内容对你有帮助,可以点赞,关注文章和专栏以及关注我哈,嘿嘿嘿我会定期更新文章的,谢谢朋友你的支持哈