Blog·Tanky WooABOUTRSS

HDOJ 1012 u Calculate e

08 Aug 2010
这篇博客是从旧博客 WordPress 迁移过来,内容可能存在转换异常。

题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1012


套公式就行了,水题~~~

#include 
#include 
#include 
using namespace std;
int factorial(int n)
{
    int sum = 1;
    for(int i = 1;i <= n;++i)
        sum = sum * i;
    return sum;
}
int main()
{
    cout<<"n"<<" "<<"e"<<endl;
    cout<<"-"<<" "<<"-----------"<<endl;
    for(int n = 0;n < 10;++n)
    {
        cout<<n<<" ";
        double sum = 0.0;
        for(int i = 0;i <= n;++i)
            sum = sum + 1.0/factorial(i);
        if(n < 3)
            cout<<sum<<endl;
        else
            cout<<fixed<<setprecision(9)<<sum<<endl;
    }
    return 0;
}