HDU u Calculate e

来源:互联网 发布:网络传播概论第四版 编辑:程序博客网 时间:2024/06/15 20:13
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 11 22 2.53 2.6666666674 2.708333333
 

 

Source
Greater New York 2000
 
 
 
看着有点吓人 但是很水 按照题目公式来就好了
 
 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 }
代码

 

原创粉丝点击