zoukankan      html  css  js  c++  java
  • PTA 水...

    习题4-2 求幂级数展开的部分和 (20分)

    已知函数e^x可以展开为幂级数1+x+x2/2!+x3/3!+⋯+xk/k!+⋯1+x+x^2 /2! + x^3 /3! + cdots + x^k /k! + cdots1+x+x
    ​2/2!+x3/3!+⋯+xk/k!+⋯。现给定一个实数x,要求利用此幂级数部分和求exe^xex的近似值,求和一直继续到最后一项的绝对值小于0.00001。
    输入格式:

    输入在一行中给出一个实数x∈[0,5]xin [0, 5]x∈[0,5]。

    输出格式:

    在一行中输出满足条件的幂级数部分和,保留小数点后四位。

    输入样例:
    1.2

    输出样例:
    3.3201

    读题还是要仔细,绝对值,感觉就错在那里吧。而且记得用fabs
    头文件是:#include <math.h>
    PS:abs的头文件是:#include<stdlib.h>

    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <algorithm>
    #include <math.h>
    #include <queue>
    #include <stack>
    using namespace std;
    #define INF 0x3f3f3f
    #define pi acos(-1.0)
    #define MAX 1010
    #define mod 9973
    #define ll long long
    int n,m;
    int main()
    {
        int i,j,k;
        double a;
        scanf("%lf",&a);
        double sum=1,t;
        t=1;
        for(i=1;;i++)
        {
            t*=i;
            sum+=pow(a,i)/t;
            if(fabs(pow(a,i)/t)<0.00001)
                break;
        }
        printf("%.4lf
    ",sum);
        return 0;
    }
  • 相关阅读:
    poj 2947 Widget Factory 夜
    poj 1222 EXTENDED LIGHTS OUT 夜
    poj 3440 Coin Toss 夜
    poj 1166 The Clocks 夜
    poj 3270 Cow Sorting 夜
    poj 3071 Football 夜
    poj 2409 Let it Bead 夜
    poj 1141 Brackets Sequence 夜
    hdu 4311 Meeting point1 夜
    poj 1026 Chipher 夜
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934478.html
Copyright © 2011-2022 走看看