欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hdu 1012 u Calculate e

程序员文章站 2022-07-15 08:47:53
...

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

hdu 1012 u Calculate e

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
题目解析:
这个题的意思就是让你输出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;
}