POJ——1517

  决定以后多练习算法,多上POJ,看过很多牛人都是这样成长起来的,也要像他们学习一下

  先挑了题简单的,省得打击自己信心,后面自己写了一个随机数生成器,就按照随机出来的数选择吧

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
...

Accepted:

 1 #include <iostream>
 2 #include <iomanip>
 3 
 4 using namespace std;
 5 double Rank(int n);
 6 int main()
 7 {
 8     double e = 0;
 9     cout <<"n e"<< endl;
10     cout <<"- -----------"<<endl;
11     for(int i = 0; i <= 9; i++)
12     {
13         e += Rank(i);
14         cout << i << " " <<setprecision(10)<< e <<endl;
15     }
16     return 0;
17 }
18 double Rank(int n)
19 {
20     double val;
21     double mul = 1;
22     if(n == 0)
23     {
24         val = 1;
25     }
26     else
27     {
28         for(int i = 1;i <= n; i++)
29         {
30             mul *= i;
31         }
32         val = 1/mul;
33     }
34     return val;
35 }
posted @ 2012-07-08 17:07  Moondark  阅读(627)  评论(0编辑  收藏  举报