HDOJ 1012 u Calculate e

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


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <iomanip>
#include <cstdio>
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;
}

发布者

Tanky Woo

Tanky Woo,[个人主页:https://tankywoo.com] / [新博客:https://blog.tankywoo.com]

发表评论

电子邮件地址不会被公开。 必填项已用*标注