HDU2199 Can you solve this equation?(二分)

February 27, 2016

题目链接

题意:在 0-100 的实数范围内找到方程的解。

二分水题

#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<cstdio>
#include<algorithm>
using namespace std;
long long y;
double f(double x){
    return 8*pow(x,4) + 7*pow(x,3) + 2*pow(x,2) + 3*x - y+6;
}

int main(){
    int t; cin >> t;
    while(t--){
        scanf("%lld", &y);
        if(f(0)*f(100) > 0){
            cout << "No solution!" << endl;
            continue;
        }
        double mid, l = 0, r = 100;
        while(r-l > 1e-6){
            mid = l+(r-l)/2;
            if(f(mid) >= 0)
                r = mid;
            else l = mid;
        }
        printf("%.4fn", mid);
    }
    return 0;
}

Profile picture

Written by Armin Li , a venture capitalist. [Weibo] [Subscribe]