zoukankan      html  css  js  c++  java
  • P1024一元三次方程求解

    P1024一元三次方程求解

    题目描述

    一元三次方程求解

    解题思路

    -100~100分为一百段

    对于每一段有以下三种情况

    1. 两端点均非零点

    则直接二分

    2. 两端点中有一零点

    如果零点在左端点,则直接跳过,因为这里每个区间使用这样的形式((left, right]),如果为右端点则不进行操作,其会在二分时输出。

    3. 两端点均为零点

    输出右端点,因为当前的右端点会在下一个区间跳过。而当前的左端点会在,上一个区间作为右端输出。

    完整代码

    #include <iostream>
    #include <cmath>
    using namespace std;
    
    double a, b, c, d;
    
    double getRes(double x) {
        return x * x * x * a + x * x * b + x * c + d;
    } // 计算方程的值
    
    int main() {
        cin >> a >> b >> c >> d;
    
        for (int i = -100; i <= 100; i ++) {
            if (getRes(i) * getRes(i + 1) <= 0) {
                double l = i, r = i + 1, mid;
                if (!getRes(l)) {
                    if (!getRes(r)) // 如果当前区间两端点均是结果
                        printf("%.2f ", r);
                    continue;
                }
                while (r - l >= 1e-6) { // 二分
                    mid = (l + r) / 2.0;
                    if (getRes(mid) * getRes(l) >= 0) l = mid;
                    else r = mid;
                }
                printf("%.2f ", r);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    poj 3258
    CodeForces 367E Sereja and Intervals
    Codeforces Round #240 (Div. 2) C Mashmokh and Numbers
    Codeforces Round #240 (Div. 2) D
    zoj 3768 Continuous Login
    2014/4/4做题感悟
    HDU 1878 欧拉回路
    HDU 3018 Ant Trip
    POJ 3694 Network
    Codeforces Round #239 (Div. 2)
  • 原文地址:https://www.cnblogs.com/mayapony/p/13986543.html
Copyright © 2011-2022 走看看