hdoj 1012 u Calculate e

u Calculate e

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


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
 
按照上边的公式 从0求到9 输出即可                注:小技巧(我看到的想记录下来,与本题无关)printf输出的时候printf("%.5g",n);可以将小数点后边的没用的0去掉如 9.231000可输出9.231
 
#include<stdio.h>
#include<string.h>
double b[20];
double fun(int x)
{
	int i;
	double sum=1.0,a=1.0;
	for(i=1;i<=x;i++)
	{
		sum=a*sum;
		a=a+1;
	}
	sum=1/sum;
	return sum;
}
int main()
{
	int n,m,j,i;
	double sum=0;
	b[0]=1;b[1]=1;
	for(i=2;i<=9;i++)
		b[i]=fun(i);
	printf("n e\n");
	printf("- -----------\n");
	for(i=0;i<=9;i++)
	{
		printf("%d ",i);
        sum+=b[i];
        if(i==2)
        {
        	printf("2.5\n");
        	continue;
        }
        if(sum==(int)(sum))
            printf("%d\n",(int)(sum));
        else
            printf("%.9lf\n",sum);
	} 
	return 0;
} 

  

posted @ 2015-10-26 13:51  非我非非我  阅读(203)  评论(0编辑  收藏  举报