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
题意:按照格式输出n取0~9时,1.0/n!的值。
分析:可以先用数组将0~9的阶乘存放起来,前三列输出的e保留的小数位与后面的不同,所以可以单独输出。
AC源代码(C语言):
1 #include<stdio.h> 2 3 int main() 4 { 5 int i,N,fn[10]={1}; 6 double e=2.5; 7 i=1; 8 while(i<10) 9 { 10 fn[i]=i*fn[i-1]; 11 i++; 12 } 13 printf("n e\n"); 14 printf("- -----------\n"); 15 printf("0 1\n"); 16 printf("1 2\n"); 17 printf("2 2.5\n"); 18 i=3; 19 while(i<10) 20 { 21 e+=1.0/fn[i]; /*如果用e+=1/fn[i],就达不到累加的效果,因为,整形除以整形要去掉小数位,切记!!!!!*/ 22 printf("%d %.9lf\n",i,e); 23 i++; 24 } 25 return 0; 26 }
2013-04-11