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): 13241 Accepted Submission(s): 5725
Problem Description
A simple mathematical formula for e is
![](http://acm.hdu.edu.cn/data/images/1012-1.gif)
where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
![](http://acm.hdu.edu.cn/data/images/1012-1.gif)
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
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333
Source
1 //输出格式要注意
2 #include <stdio.h>
3 #include <string.h>
4 #include <stdlib.h>
5
6
7 int dp[100];
8 double sum[100];
9
10
11 void fun( int n)
12 {
13 int i;
14
15 dp[0] = 1;
16
17 for (i = 1; i <= n; i++)
18 dp[i] = dp[i-1] * i;
19
20 }
21
22 int main( )
23 {
24 int i, j, N;
25 memset(dp, 0, sizeof(dp));
26 memset(sum, 0, sizeof(sum));
27 fun(10);
28 sum[0] = 1;
29 for(i = 1; i <= 9; i++)
30 sum[i] = sum[i - 1] + 1.0 / dp[i];
31 printf("n e\n- -----------\n");
32 for(i = 0; i <= 9; i++) {
33 if(sum[i] == (int) sum[i])
34 printf("%d %d\n",i, int(sum[i]) );
35 else if ( i == 2)
36 printf("%d %.1lf\n", i, sum[i]);
37 else
38 printf("%d %.9lf\n",i, sum[i]);
39 }
40 return 0;
41 }
posted on 2011-08-25 20:55 more think, more gains 阅读(190) 评论(0) 编辑 收藏 举报