hud 1012 u Calculate e (acm)

u Calculate e

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


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
Greater New York 2000



 
//-------------------------------------------打印e值表
此题很水的,把题目意思看懂了基本可以解决。
代码如下:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
using namespace std;
int jc(int l)
{
    if(l==0||l==1)
        return 1;
    else return l*jc(l-1);
}
int main()
{
   int n,a[11];


   memset(a,0,sizeof(a));
   for(int i=0;i<=9;i++)
   {
       a[i]=jc(i);
   }printf("n e\n");
       printf("- -----------\n");
   for(int n=0;n<=9;n++)
   {  double e=0;
       if(n<0||n>9) break;//判断数值范围


       for(int i=0;i<=n;i++)//计算和值
       {
         e+=(double)1/a[i];
       }
       if(n==0||n==1)                                    //判断输出格式
        printf("%d %.0f\n",n,e);
       else if(n==2)
        printf("%d %.1f\n",n,e);
       else
       printf("%d %.9lf\n",n,e);
   }
   return 0;
}

你可能感兴趣的:(算法,ACM,杭电oj,hud1012)