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

时间: 2025-01-18 13:15:40

u Calculate e的相关文章

HDU 2114 Calculate S(n)(公式)

Calculate S(n) Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9640    Accepted Submission(s): 3492 Problem Description Calculate S(n). S(n)=13+23 +33 +......+n3 . Input Each line will contain

HDU 1012 u Calculate e【暴力打表,水】

u Calculate e Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 46844    Accepted Submission(s): 21489 Problem Description A simple mathematical formula for e is where n is allowed to go to infini

线段树 + 矩阵 --- ZOJ 3772 Calculate the Function

Calculate the Function Problem's Link:   http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 Mean: 略 analyse: 简单的线段树维护矩阵. 矩阵乘法的结合律(a * b * c == a * (b * c)),注意矩阵乘法不满足分配率(a *b != b * a). 令 M[x] = [1 A[x]]              [1     0 ] ,那么有 [ F

Exercise 3.3 Calculate a discounted price

Exercise 3-3. Write a program that will calculate the price for a quantity entered from the keyboard, given that the unit price is $5 and there is a discount of 10 percent for quantities over 30 and a 15 percent discount for quantities over 50. 1 //

Calculate the formula

Problem Description You just need to calculate the sum of the formula: 1^2+3^2+5^2+……+ n ^2. Input In each case, there is an odd positive integer n. Output Print the sum. Make sure the sum will not exceed 2^31-1 Sample Input 3 Sample Output 10 用普通的做法

hdu 1012:u Calculate e(数学题,水题)

u Calculate e Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 28686    Accepted Submission(s): 12762 Problem Description A simple mathematical formula for e iswhere n is allowed to go to infinit

Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5

Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of its dependencies could not be resolved 此问题的解决: 1.要确认spring sts已经设置代理 2.要确认windows -> Preferences -> maven 的settings.xml文件中,设置代理. 3.C:\Users\ttt\.m2\

HDU2139 Calculate the formula【水题】

Calculate the formula Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7441    Accepted Submission(s): 2284 Problem Description You just need to calculate the sum of the formula: 1^2+3^2+5^2+--+

HDU2114 Calculate S(n)【数学】

Calculate S(n) Time Limit: 10000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8423    Accepted Submission(s): 3060 Problem Description Calculate S(n). S(n)=13+23 +33 +......+n3 . Input Each line will contain

ZOJ 3772 Calculate the Function 线段树+矩阵

Calculate the FunctionTime Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-04-09) Description You are given a list of numbers A1A2 .. AN and M queries. For the i-th query