zoukankan      html  css  js  c++  java
  • 程序设计与算法(二)算法基础》《第四周 二分》二分法求函数的零点 4142

    描述

    有函数:

    f(x) = x5 - 15 * x4+ 85 * x3- 225 * x2+ 274 * x - 121

    已知 f(1.5) > 0 , f(2.4) < 0 且方程 f(x) = 0 在区间 [1.5,2.4] 有且只有一个根,请用二分法求出该根。

    输入无。输出该方程在区间[1.5,2.4]中的根。要求四舍五入到小数点后6位。

    /*
    http://bailian.openjudge.cn/practice/4142/
    4142:二分法求函数的零点
    */
    #include<cstdio>
    #include<cmath>
    using namespace std;
    
    #define EPS 1e-6
    double f(double x)
    {
        return pow(x, 5) - 15 * pow(x, 4) + 85 * pow(x, 3) - 225 * pow(x, 2) + 274 * x - 121;
    }
    int main()
    {
        double root, left = 1.5, right = 2.4, y;
        int times = 1;
        root = left + (right - left) / 2;
        y = f(root);
        while (fabs(y) > EPS)
        {
            if (y > 0)
            {
                left = root;
            }
            else
            {
                right = root;
            }
            root = left + (right - left) / 2;
            y = f(root);
            times++;
        }
        printf("%.6lf
    ", root);
        //printf("%d
    ", times);
    }
  • 相关阅读:
    ZoomBar 设计
    旋转toast 自定义toast方向,支持多个方向的显示,自定义View
    NA
    ISCSI共享
    DFS序
    矩阵快速幂
    SOJ4389 川大贴吧水王 队列
    ST表学习总结
    HDU 5724 Chess(SG函数)
    2017 计蒜之道 初赛 第一场 A、B题
  • 原文地址:https://www.cnblogs.com/focus-z/p/11494778.html
Copyright © 2011-2022 走看看