首页 > ACM题库 > HDU-杭电 > HDU 4423-Simple Function[解题报告]HOJ
2015
07-16

HDU 4423-Simple Function[解题报告]HOJ

Simple Function

问题描述 :

Knowing that x can be any real number that x2 + Dx + E ≠ 0. Now, given the following function:
The Little Girl who Picks Mushrooms

What is the range of y?

输入:

The first line contains a single integer T (T ≤ 10000), indicating that there are T cases below.
Each case contains five integers in a single line which are values of A, B, C, D and E (-100 ≤ A, B, C, D, E ≤ 100).

输出:

The first line contains a single integer T (T ≤ 10000), indicating that there are T cases below.
Each case contains five integers in a single line which are values of A, B, C, D and E (-100 ≤ A, B, C, D, E ≤ 100).

样例输入:

5
1 1 1 2 3
0 1 0 1 -10
-3 -1 0 -1 -1
0 0 0 0 0
1 3 0 2 0

样例输出:

[0.3170, 1.1830]
(-INF, INF)
(-INF, -1.8944] U [-0.1056, INF)
[0.0000, 0.0000]
(-INF, 1.0000) U (1.0000, 1.5000) U (1.5000, INF)

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
#define EPS 1e-6

int compare_real(double d){
    if (d > EPS)
        return 1;
    else if (d < -EPS)
        return -1;
    else
        return 0;
}

int main(){
    int T;
    cin>>T;
    while(T--){
        int A, B, C, D, E;
        scanf("%d%d%d%d%d", &A, &B, &C, &D, &E);
        B = B - A*D;
        C = C - A*E;
        double AA = A;

        if (B == 0){
            if (C == 0)
                printf("[%.4f, %.4f]\n", AA, AA);
            else{
                double m = -D*D/4.0 + E;
                if (compare_real(m) > 0){
                    if (C > 0)
                        printf("(%.4f, %.4f]\n", AA, C / m + AA);
                    else
                        printf("[%.4f, %.4f)\n", C / m + AA, AA);
                }
                else if (compare_real(m) == 0){
                    if (C > 0)
                        printf("(%.4f, INF)\n", AA);
                    else
                        printf("(-INF, %.4f)\n", AA);
                }
                else{
                    if (C > 0)
                        printf("(-INF, %.4f] U (%.4f, INF)\n", C / m + AA, AA);
                    else
                        printf("(-INF, %.4f) U [%.4f, INF)\n", AA, C / m + AA);
                }
            }
        }
        else{
            double x1 = -1.0*C/B;
            if (compare_real(x1*x1+D*x1+E) == 0){
                double x2 = -D - x1;
                if (compare_real(x1-x2) == 0)
                    printf("(-INF, %.4f) U (%.4f, INF)\n", AA, AA);
                else{
                    double py = B / (x1 - x2);
                    if (compare_real(py) < 0)
                        printf("(-INF, %.4f) U (%.4f, %.4f) U (%.4f, INF)\n", py+AA, py+AA, AA, AA);
                    else
                        printf("(-INF, %.4f) U (%.4f, %.4f) U (%.4f, INF)\n", AA, AA, py+AA, py+AA);
                }
            }
            else{
                double D2 = D - 2.0*C/B;
                double E2 = 1.0*C*C/B/B - 1.0*C*D/B + E;
                if (compare_real(E2) < 0)
                    printf("(-INF, INF)\n");
                else{
                    double p = D2 + 2 * sqrt(E2);
                    double q = D2 - 2 * sqrt(E2);
                    if (B > 0){
                        if (compare_real(p)>0 && compare_real(q) > 0)
                            printf("(-INF, %.4f] U [%.4f, INF)\n", B/p + AA, B/q + AA);
                        else if (compare_real(q) == 0)
                            printf("(-INF, %.4f]\n", B/p + AA);
                        else if (compare_real(p)>0 && compare_real(q) < 0)
                            printf("[%.4f, %.4f]\n", B/q + AA, B/p + AA);
                        else if (compare_real(p) == 0)
                            printf("[%.4f, INF)\n", B/q + AA);
                        else
                            printf("(-INF, %.4f] U [%.4f, INF)\n", B/p + AA, B/q + AA);
                    }
                    else{
                        if (compare_real(p)>0 && compare_real(q) > 0)
                            printf("(-INF, %.4f] U [%.4f, INF)\n", B/q + AA, B/p + AA);
                        else if (compare_real(q) == 0)
                            printf("[%.4f, INF)\n", B/p + AA);
                        else if (compare_real(p)>0 && compare_real(q) < 0)
                            printf("[%.4f, %.4f]\n", B/p + AA, B/q + AA);
                        else if (compare_real(p) == 0)
                            printf("(-INF, %.4f]\n", B/q + AA);
                        else
                            printf("(-INF, %.4f] U [%.4f, INF)\n", B/q + AA, B/p + AA);
                    }
                }
            }
        }
    }
    return 0;
}