hdu1012 u Calculate e

没什么好讲的

u Calculate e
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36073 Accepted Submission(s): 16277

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

#include <cmath>
#include <cstdio>

int main(){
    printf("n e\n");
    printf("- -----------\n");
    printf("0 1\n");
    double sum=1;
    int product=1;
    for(int i=1;i<=9;i++){
        product*=i;
        sum+=1/(double)product;
        if(i>=3)
            printf("%d %.9f\n",i,sum);
        else if(i==2)
            printf("%d %.1f\n",i,sum);
        else if(i==1)
             printf("%d %.0f\n",i,sum);
    }
    return 0;
}

你可能感兴趣的:(C++,ACM,水题)