码迷,mamicode.com
首页 > 其他好文 > 详细

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

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

标签:hdu   poj   数学   

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(简单阶乘)

标签:hdu   poj   数学   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!