hdu - 2199 - Can you solve this equation?(二分)

题意:求8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y在[0, 100]上的解。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2199

——>>简单二分。

发现,在hust上用函数写法0ms过,用define写法15ms过,而这两种写法在hdu上都是0ms过。奇葩~

函数写法:

#include <cstdio>
#include <cmath>

using namespace std;

const double eps = 1e-14;
double Y;
double F(double x)
{
    return 8*pow(x, 4) + 7*pow(x, 3) + 2*pow(x, 2) + 3*x + 6 - Y;
}
int main()
{
    int T, i;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lf", &Y);
        double f0 = F(0), f100 = F(100);
        if(f0 > eps || f100 < -eps) printf("No solution!\n");
        else
        {
            double x = 0, y = 100, m;
            for(i = 0; i < 100; i++)
            {
                m = x + (y - x) / 2;
                if(F(m) < 0) x = m;
                else y = m;
            }
            printf("%.4lf\n", m);
        }
    }
    return 0;
}

define写法:

#include <cstdio>
#include <cmath>
#define F(x) (8*pow(x, 4) + 7*pow(x, 3) + 2*pow(x, 2) + 3*x + 6 - Y)

using namespace std;

const double eps = 1e-14;
double Y;

int main()
{
    int T, i;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lf", &Y);
        double f0 = F(0), f100 = F(100);
        if(f0 > eps || f100 < -eps) printf("No solution!\n");
        else
        {
            double x = 0, y = 100, m;
            for(i = 0; i < 100; i++)
            {
                m = x + (y - x) / 2;
                if(F(m) < 0) x = m;
                else y = m;
            }
            printf("%.4lf\n", m);
        }
    }
    return 0;
}


你可能感兴趣的:(hdu - 2199 - Can you solve this equation?(二分))