HDU u Calculate e

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.
 

 

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
 
 
 
看着有点吓人 但是很水 按照题目公式来就好了
 
 1 /*
 2     前三个数 直接打印就行 
 3 */
 4 #include<cstdio>
 5 #include<iostream>
 6 
 7 using namespace std;
 8 
 9 int main() {
10     printf("n e\n");
11     printf("- ");
12     for(int i=1;i<=11;i++) printf("-");
13     printf("\n");
14     printf("0 1\n");
15     printf("1 2\n");
16     printf("2 2.5\n");
17     for(int i=3;i<=9;i++) {
18         double ans=.0,k=1;
19         int t=0;
20         printf("%d ",i);
21         for(int j=0;j<=i;j++) {
22             ans+=(1/k);
23             t++;
24             k*=t;
25         }
26         printf("%.9lf\n",ans);
27     }
28     return 0;
29 }
代码

 

posted @ 2017-07-12 15:42  拿叉插猹哈  阅读(95)  评论(0编辑  收藏  举报