u Calculate e

http://acm.zju.edu.cn/show_problem.php?pid=1113

Time limit: 1 Seconds

Memory limit: 32768K

1. Background

A simple mathematical formula for e is

<<latex: 执行失败 [Missing parentheses in call to 'print'. Did you mean print(...)?] (see also the log)>>

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

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

3. Sample Output

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

Problem Source: Greater New York 2000


   1 /*Written by 洪峰*/
   2 #include<iostream>
   3 using namespace std;
   4 
   5 int main()
   6 {
   7     long double t1=6, e=2.5;
   8     int n;
   9     cout<<"n"<<" "<<"e"<<endl;
  10     cout<<"- -----------"<<endl;
  11     cout<<0<<" "<<1<<endl;
  12     cout<<1<<" "<<2<<endl;
  13     cout<<2<<" "<<2.5<<endl;
  14     for (n=3;n<10;n++)
  15     {
  16         e+=1/t1;
  17         t1*=(n+1);
  18         cout.precision(9);
  19         cout.setf(ios::fixed);
  20         cout<<n<<" "<<e<<endl;
  21     }
  22     return 0;
  23 }

zju1113 (2008-02-23 15:34:51由localhost编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.