首页 > 其他 > 详细

poj 1517 & hdu 1012 u Calculate e(简单阶乘)

时间:2014-08-18 16:28:13      阅读:333      评论:0      收藏:0      [点我收藏+]

POJ链接 :http://poj.org/problem?id=1517

HDU链接:http://acm.hdu.edu.cn/showproblem.php?pid=1012


Description

A simple mathematical formula for e is 
e=Σ0<=i<=n1/i!

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

Input

No input

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 Input

no input

Sample Output

n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
...

Source



注意:poj上用G++交WA了,用C++AC,HDU上则不存在此问题。


代码如下:

#include <cstdio>
int Fac(int n)
{
    int s=1;
    for(int i=1; i<=n; i++)
        s*=i;
    return s;
}
int main()
{
    double sum;
    printf("n e\n- -----------\n");
    for(int i = 0; i <= 9; i++)
    {
        sum = 1;
        for(int j = 1; j <= i; j++)
        {
            sum+=1.0/Fac(j);
        }
        if(i <= 1)
            printf("%d %.0lf\n",i,sum);
        else if(i == 2)
            printf("2 2.5\n");
        else if(i > 2)
            printf("%d %.9lf\n",i,sum);
    }
    return 0;
}


poj 1517 & hdu 1012 u Calculate e(简单阶乘),布布扣,bubuko.com

poj 1517 & hdu 1012 u Calculate e(简单阶乘)

原文:http://blog.csdn.net/u012860063/article/details/38661045

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!