Calculate( HDU - 1012 )

A - Calculate(5p)

 来源:HDU - 1012 

Time limit:1000 ms                Memory limit:32768 kB

OS:Windows                         Source:Greater New York 2000

Problem Description

自然常数e有一个简单的公式(其实是exp在x=1位置的泰勒展开式) 

 

n趋于无穷时,整个式子的值将趋于e的值。你的任务时是计算n取0-9的时候,整个式子的值分别是多少。程序没有输入 

Output

输出n分别取0到9时,整个式子的值,格式如下

Sample Output

n e
- -----------
0 1
1 2
2 2.5
3 2.666666667
4 2.708333333

Hint:题目中给的样例输出,包括n,e和分界线。自己因为这里wa了几次。同时前三个直接用cout输出的,后边用的printf,有更好办法的小伙伴欢迎留言。

代码如下:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int jc(int x)
{
    if(x == 0) return 1;
    int num = 1;
    while(x)
    {
        num *= x;
        x -= 1;
    }
    return num;
}
int main()
{
    int i, j;
    double a[10] = {0};
    printf("n e\n");
    printf("- -----------\n");
    for(i = 0; i <= 9; i++)
    {
        for(j = i; j >= 0; j--)
        {
            a[i] += (double)1/jc(j);
        }
        if(i <= 2)
        {
            cout<< i << ' ' << a[i] << endl;
        }
        else
        {
            printf("%d %.9lf\n", i, a[i]);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

WMYBlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值