u Calculate e

问题陈述:

  杭州电子科技大学 HANGZHOU DIANZI UNIVERSITY Online Judge Problem - 1012

 

问题解析:

  简单题,注意输出格式。引入<iomainip>头文件,调用setprecision()函数设置精度。

 

代码详解:

 1 #include <iostream>

 2 #include <iomanip>

 3 #include <cstdio>

 4 

 5 using namespace std;

 6 

 7 int factorial(int);

 8 

 9 int main()

10 {

11     int i, n;

12     double e;

13     cout << "n" << " " << "e" << endl;

14     cout << "-" << " " << "-----------" << endl;

15     for(n=0; n<=9; n++) {

16         e = 0;

17         for(i=0; i<=n; i++) {

18             e += 1.0/factorial(i);

19         }

20         if(n < 3) {

21             cout << n << " " << e << endl;

22         }else {

23             cout << fixed << setprecision(9) << n << " " << e << endl;

24         }

25     }

26     return 0;

27 }

28 

29 int factorial(int n) {

30     if(n == 0) {

31         return 1;

32     }else {

33         return n * factorial(n-1);

34     }

35 }

 

转载请注明出处:http://www.cnblogs.com/michaelwong/p/4287200.html

你可能感兴趣的:(ca)