HDU 1012 uCalculate e

来源:互联网 发布:中医之钥 知乎 编辑:程序博客网 时间:2024/04/28 19:08

uCalculate e

Time Limit: 2000/1000 MS (Java/Others)    MemoryLimit: 65536/32768 K (Java/Others)
Total Submission(s): 18865    Accepted Submission(s): 8226


Problem Description

A simplemathematical formula for e is

                            


where n is allowed to go to infinity. This can actually yield very accurateapproximations of e using relatively small values of n.

 

 

Output

Output theapproximations of e generated by the above formula for the values of n from 0to 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

Greater New York 2000

 

 

Recommend

JGShining

 

解题思路:本题思路非常明确,主要考察的是预处理和精度控制,特别要注意的是输入为8,输出的结果是2.182787702.18278768四舍五入而来),%g可以控制浮点型不输出最后面的0

#include <stdio.h>double a[10]={1,2,2.5};long f[10]={0,1,2,6,24,120,720,5040,40320,362880};int main(int argc, char *argv[]){    int i=0;    for(i=3;i<=9;i++)    {        a[i]=a[i-1]+1.0/f[i];    }    printf("n e\n");    printf("- -----------\n");    for(i=0; i<=2;i++)    {        printf("%d %g\n",i,a[i]);    }    for(i=3; i<=9;i++)    {        printf("%d %.9lf\n",i,a[i]);    }    return 0;}


 

原创粉丝点击