hdu 2199又是一道二分答案的题

这都是今天做的第三道二分答案的题了。。。这题精度控制要求也比较高,我是把eps设成1e-8才过的1e-6都过不了。。。

/*

 * hdu2199/win.cpp

 * Created on: 2012-11-2

 * Author    : ben

 */

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <ctime>

#include <iostream>

#include <algorithm>

#include <queue>

#include <set>

#include <map>

#include <stack>

#include <string>

#include <vector>

#include <deque>

#include <list>

#include <functional>

#include <numeric>

#include <cctype>

using namespace std;

const double eps = 0.00000001;

const double LOW = 6 - eps;

const double HIGH = 807020306 + eps;

inline double getresult(double x) {

    double xx = x * x;

    double xxxx = xx * xx;

    return 8 * xxxx + 7 * x * xx + 2 * xx + 3 * x + 6;

}

double getans(double y) {

    double low = 0, high = HIGH, mid;

    while(high - low > eps) {

        mid = (low + high) / 2;

        if(getresult(mid) - y < eps) {

            low = mid;

        }else {

            high = mid;

        }

    }

    return mid;

}

int main() {

#ifndef ONLINE_JUDGE

    freopen("data.in", "r", stdin);

#endif

    int T;

    double y;

    scanf("%d", &T);

    while(T--) {

        scanf("%lf", &y);

        if(y < LOW || y > HIGH) {

            puts("No solution!");

            continue;

        }

        double ans = getans(y);

        printf("%.4f\n", ans);

    }

    return 0;

}

你可能感兴趣的:(HDU)