zoukankan      html  css  js  c++  java
  • 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
    ");
    	printf("- -----------
    ");
    	for(i=0;i<=9;i++)
    	{
    		printf("%d ",i);
            sum+=b[i];
            if(i==2)
            {
            	printf("2.5
    ");
            	continue;
            }
            if(sum==(int)(sum))
                printf("%d
    ",(int)(sum));
            else
                printf("%.9lf
    ",sum);
    	} 
    	return 0;
    } 
    

      

  • 相关阅读:
    Session服务器配置指南与使用经验
    string和byte[]的转换 (C#)
    错误1已授予对“SqlAccess...的友元访问解决方法
    网络视频会议 二
    Editplus配置环境变量
    TSC 条码打印机 Dll 说明
    源码C#事例网址
    C#中的日志类
    分页事例 比较好的
    dotnet 网络编程 tcp
  • 原文地址:https://www.cnblogs.com/tonghao/p/4910996.html
Copyright © 2011-2022 走看看