u Calculate e
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36110 Accepted Submission(s): 16298
Problem Description
A simple mathematical formula for e is
![](http://acm.hdu.edu.cn/data/images/1012-1.gif)
where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
![](http://acm.hdu.edu.cn/data/images/1012-1.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
Source
Recommend
这道题没有输入,开始看到的时候怎么看怎么觉得怪异~题目不难,读懂题就好,注意输出,前三个数的输出后后面的输出不同。
题意:按题目给的公式,输出n从0到9的结果,前3个lg输出,后面的保留9位小数。
附上代码:
1 #include <stdio.h> 2 double add(double t) 3 { 4 double s=1; 5 int i; 6 if(t==0) //0!=1 7 return 1; 8 for(i=1; i<=t; i++) //求n! 9 s*=i; 10 return s; 11 } 12 int main() 13 { 14 double e=0; 15 int i,j; 16 printf("n e "); 17 printf("- ----------- "); 18 printf("0 1 "); 19 for(i=1; i<=9; i++) 20 { 21 e=0; 22 for(j=0; j<=i; j++) 23 e=e+1/add(j); 24 if(i<=2) //输出需要分开讨论 25 printf("%d %lg ",i,e) ; 26 else 27 printf("%d %.9lf ",i,e) ; 28 } 29 return 0; 30 }