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
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#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 @   非我非非我  阅读(203)  评论(0编辑  收藏  举报
编辑推荐:
· 从二进制到误差:逐行拆解C语言浮点运算中的4008175468544之谜
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
阅读排行:
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
· C# 13 中的新增功能实操
· Supergateway:MCP服务器的远程调试与集成工具
· Vue3封装支持Base64导出的电子签名组件
点击右上角即可分享
微信分享提示