hdu 1012 u Calculate e

来源:互联网 发布:男士 长袖 知乎 编辑:程序博客网 时间:2024/06/09 22:47

u Calculate e

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45773    Accepted Submission(s): 21027


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
题目解析:
这个题的意思就是让你输出n从0到9公式求出的值,是一个非常简单的题,但是要注意输出。
代码:
#include<iostream>#include<cstdio>using namespace std;int main(){    int i,j,jiecheng[10];    double a[10];    cout<<"n e"<<endl<<"- -----------"<<endl;    for(i=0; i<=9; i++)    {        for(j=0; j<i; j++)        {            jiecheng[0]=1;            jiecheng[i]=i*jiecheng[i-1];        }        a[0]=1;        a[i]=a[i-1]+1.0/jiecheng[i];        if(i==0) cout<<"0"<<" "<<"1"<<endl;        if(i==1) cout<<"1"<<" "<<"2"<<endl;        if(i==2) cout<<"2"<<" "<<"2.5"<<endl;        if(i>=3)        {            cout<<i<<" ";            printf("%.9lf\n",a[i]);        }    }    return 0;}

0 0
原创粉丝点击