PTA 实验4-2-2 求e的近似值 (15point(s)) Easy only once *和7-3同问题

基本思想:

仍然是按照展开式的计算问题;

关键点:

无;

#include<stdlib.h>
#include<stdio.h>
#include<iostream>
using namespace std;

double factor(double x) {
    if (x == 1.0)
        return 1.0;
    double sum = 1.0;
    while (x != 1) {
        sum *= x--;
    }
    return sum;
}

void change(int e) {
    double sum = 1;
    int index = 1;
    while (e > 0) {
        sum += 1 / factor(index++);
        e--;
    }
    printf("%.8lf", sum);
}

int main() {
    int n;
    scanf("%d", &n);
    change(n);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/songlinxuan/p/12360751.html
今日推荐