u Calculate e
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36307 Accepted Submission(s): 16399
Problem Description
A simple mathematical formula for e is
e=Σ1/i!(i=0~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
Source
Greater New York 2000
题目大意:
给出一个e的表达式,即是求前n项的阶乘的导数之和。表达式为:e=1+1/1!+1/2!+1/3!+1/4!+……+1/9!;输出参考输出样例,依次输出n从0至9的结果。
解题思路:
首先按照输出及简单运算可知n等于0、1、2的时候对应结果为1、2、2.5;将此直接输出结果,此后结果含有多项小数,保留小数点后9位;对此可先求出i的阶乘,然后代入表达式,将运算结果输出即可。
AC代码:
#include<stdio.h>
double factorial(int n) //求阶乘函数
{
double num=1;
for(int i=1;i<=n;i++)
num*=i;
return num;
}
int main()
{
double sum; //对前面可直接计算出结果的单独输出
printf("n e\n- -----------\n0 1\n1 2\n2 2.5\n");
sum=2.5;
for(int i=3;i<=9;i++) //将运算结果逐个计算并输出
printf("%d %.9lf\n",i,sum+=1/factorial(i));
return 0;
}