题目链接:
点击打开题目链接
题复制:
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 1 1 2 2 2.5 3 2.666666667 4 2.708333333
题目大意:
计算以上公式的n从1到9的计算结果;按题目格式;
不会的话前几个直接复制,n从3开始计算;
以后保留9位小数;
代码:
#include<stdio.h>
double nj(int n)
{
int i,sum;
for(i=1,sum=1;i<=n;i++)
sum*=i;
return sum;
}
main()
{
int i,j;
double e=0.0;
printf("n e\n- -----------\n0 1\n1 2\n2 2.5\n");
for(i=3,e=2.5;i<10;i++)
{
e+=1.0/nj(i);
printf("%d %.9lf\n",i,e);
}
return 0;
}
运行结果: