hdu-1012 u Calculate e

来源:互联网 发布:软件构架实践 课程 编辑:程序博客网 时间:2024/06/06 00:43

题目来源:hdu-1012

u Calculate eTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 36307    Accepted Submission(s): 16399Problem DescriptionA simple mathematical formula for e ise=Σ1/i!(i=0~n);where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.OutputOutput 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 Outputn e- -----------0 11 22 2.53 2.6666666674 2.708333333SourceGreater New York 2000 

题目大意:
给出一个e的表达式,即是求前n项的阶乘的导数之和。表达式为:e=1+1/1!+1/2!+1/3!+1/4!+……+1/9!;输出参考输出样例,依次输出n从0至9的结果。
解题思路:
首先按照输出及简单运算可知n等于0、1、2的时候对应结果为1、2、2.5;将此直接输出结果,此后结果含有多项小数,保留小数点后9位;对此可先求出i的阶乘,然后代入表达式,将运算结果输出即可。
AC代码:

#include<stdio.h>double factorial(int n)     //求阶乘函数 {    double num=1;    for(int i=1;i<=n;i++)        num*=i;    return num;}int main(){    double sum;             //对前面可直接计算出结果的单独输出     printf("n e\n- -----------\n0 1\n1 2\n2 2.5\n");    sum=2.5;    for(int i=3;i<=9;i++)   //将运算结果逐个计算并输出         printf("%d %.9lf\n",i,sum+=1/factorial(i));    return 0;}
0 0