码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1012 u Calculate e

时间:2015-08-26 17:24:57      阅读:103      评论:0      收藏:0      [点我收藏+]

标签:

u Calculate e

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 36110    Accepted Submission(s): 16298


Problem Description
A simple mathematical formula for e is

技术分享

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

 

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 Output
n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
 

 

Source
 

 

Recommend
JGShining   |   We have carefully selected several similar problems for you:  1032 1022 1071 1006 1048 
 
这道题没有输入,开始看到的时候怎么看怎么觉得怪异~题目不难,读懂题就好,注意输出,前三个数的输出后后面的输出不同。
 
题意:按题目给的公式,输出n从0到9的结果,前3个lg输出,后面的保留9位小数。
 
附上代码:
 1 #include <stdio.h>
 2 double add(double t)
 3 {
 4     double s=1;
 5     int i;
 6     if(t==0)   //0!=1
 7         return 1;
 8     for(i=1; i<=t; i++)  //求n!
 9         s*=i;
10     return s;
11 }
12 int main()
13 {
14     double e=0;
15     int i,j;
16     printf("n e\n");
17     printf("- -----------\n");
18     printf("0 1\n");
19     for(i=1; i<=9; i++)
20     {
21         e=0;
22         for(j=0; j<=i; j++)
23             e=e+1/add(j);
24         if(i<=2)              //输出需要分开讨论
25             printf("%d %lg\n",i,e) ; 
26         else
27             printf("%d %.9lf\n",i,e) ;
28     }
29     return 0;
30 }

 

hdu 1012 u Calculate e

标签:

原文地址:http://www.cnblogs.com/pshw/p/4760739.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!