首页 > 技术文章 > HDU 2199 Can you solve this equation?

FTA-Macro 2017-07-31 17:57 原文

Can you solve this equation?

 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Problem Description
Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y,can you find its solution between 0 and 100; Now please try your lucky.
Input
The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);
Output
For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.
Sample Input
2 100 -4
 
Sample Output
1.6152 No solution!

 

题目大意:给出Y的值,求是否存在X使得8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6 == Y。

大致思路:简单的二分题,详见代码。

#include<iostream>
#include<cstdio>
using namespace std;
int judge(double x,double y)//找到一个x使得等式无限逼近0
{
    if(y-6.0-3.0*x-2.0*x*x-7.0*x*x*x-8.0*x*x*x*x<0)//这里可以用pow()函数表示次方
        return 1;
    else return 0;
}
int main()
{
    int T;
    double y,mid;
    cin>>T;
    while(T--)
    {
        double lo=0,hi=100;
        cin>>y;
        if(y<6||y>807020306)//y的最大值就是X取100的时候,即807020306
            cout<<"No solution!"<<endl;
        else
        {
            while(hi-lo>1e-10)//每次二分直接取当前的lo和hi作mid,所以hi是不能直接大于lo的
            {
                mid=(hi+lo)/2.0;
                if(judge(mid,y))
                    hi=mid;
                else lo=mid;
            }
            printf("%.4lf\n",mid);//保留小数点后4位
        }
    }
    return 0;
}

 

推荐阅读