Problem Description
A simple mathematical formula for e is
where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
![](https://i-blog.csdnimg.cn/blog_migrate/bcc4c5ac292d59aef31fcf3227405456.gif)
where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
Output
Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
Sample Output
n e - ----------- 0 1 1 2 2 2.5 3 2.666666667 4 2.708333333
水题,代码如下
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int i,j,k,t;
double e;
cout<<"n e"<<endl<<"- -----------"<<endl;
for(i=0;i<=9;i++)//0到9 循环
{
e=0;
for(j=0;j<=i;j++)//求Σ
{
t=1;
for(k=1;k<=j;k++)//求阶乘
{
t*=k;
}
e+=1/(double)t;
}
if(i<=3)
cout<<i<<" "<<setprecision(10)<<e<<endl;
else
{
cout<<setiosflags(ios::fixed);
cout<<i<<" "<<setprecision(9)<<e<<endl;//保留9位小数
}
}
return 0;
}