【HDOJ】2199 Can you solve this equation?

二分查找,注意精度,1e-10可解。

#include <iostream>
#include <cmath>
using namespace std;

double fun(double x) {
    return 8.0*pow(x, 4.0)+7.0*pow(x, 3.0)+2.0*pow(x, 2.0)+3.0*x+6.0;
}


int main() {
    int case_n;
    double y;
    double l, r, tmp;

    cin >>case_n;

    while (case_n--) {
        cin >>y;
        l = 0.0;
        r = 100.0;
        if (fun(l)<=y && y<=fun(r)) {
            while (r-l > 1e-7) {
                tmp = (l+r) / 2;
                double val = fun(tmp);
                if (val > y) {
                    r = tmp - 1e-8;
                } else {
                    l = tmp + 1e-8;
                }
            }
            printf("%.4lf\n", (l+r) / 2);
        } else {
            printf("No solution!\n");
        }
    }

    return 0;
}

 

posted on 2014-03-10 17:35  Bombe  阅读(117)  评论(0编辑  收藏  举报

导航