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.
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 11 22 2.53 2.6666666674 2.708333333
题意:
就是求e的值,而e的值是根据上面的那个公式求出来的n越大则e的值越精确,题中已经给了5个输出案例,按照上面的形式输出n=0-9时e的值。
代码:
#include<stdio.h>
int g(int a);
int main()
{
int i;
double sum=0;
for(i=0;i<9;i++)
{
sum=sum+1.0/g(i);
printf("%.9lf\n",sum);
}
return 0;
}
int g(int a)
{
if(a==0||a==1)
return 1;
else
return g(a-1)*a;
}
分析:
果断的暴力输出- -!
先把所有的数算出来然后输出。。