其他分享
首页 > 其他分享> > 计算方法 - 求近似值

计算方法 - 求近似值

作者:互联网

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define ll long long
const double m = 1e8;
const double esp = 0.5e-3;

double i1[20] = {0.0}, i2[20] = {0.0};

double f(double x) {
    double t = 1.0;
    while(abs(x) < m) {
        x *= 10;
        t *= 10;
    }
    int a = (int)x;
    if( (abs(a) % 10) >= 5) {
        if(a > 0) a += 10;
        else if(a < 0) a -= 10;
    }
    a /= 10; t /= 10;
    x = (double)a / t;
    return x;
}

double I1(int n) {
    if(n == 0) {
        i1[0] = 0.63212056;
        return i1[0];
    }
    i1[n] = f(1 - n * I1(n-1));
    return i1[n];
}

double I2(int n) {
    if(n == 14) {
        i2[14] = 0.05;
        return i2[14];
    }
    i2[n]  = (1 - I2(n+1)) / (n+1);
    return i2[n];
}

int main() {
    int n;
    cin >> n;
    I1(14);
    I2(0);
    if(n == 1)
    for(int i = 1; i <= 14; ++i) {
        printf("%d, %.8lf\n",i,i1[i]);
    }
    if(n == 2)
    for(int i = 13; i >= 0; --i) {
        printf("%d, %.8lf\n",i,i2[i]);
    }
}


标签:10,return,int,double,i2,i1,近似值,计算方法
来源: https://blog.csdn.net/weixin_45919985/article/details/117964580