杭电2199.Can you solve this equation?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了杭电2199.Can you solve this equation?相关的知识,希望对你有一定的参考价值。

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!
 
 
本题涉及二分法
 
 
#include<stdio.h>
#include<math.h>

double ans(double a)//计算(8*x^4 + 7*x^3 + 2*x^2 + 3*x + 6)
{
    return (8*a*a*a*a+7*a*a*a+2*a*a+3*a+6.0);
}
double ans(double);//函数的声明
int main()
{
    double l,r,y,mid;
    int n,flag;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%lf",&y);
//l为左端,r为右端;
        l=0;
        flag=0;
        r=100;
//以下为二分法的精髓
        while(l<r)
        {
            mid=(l+r)/2.0;
            if(fabs(ans(mid)-y)<=1e-6)
            {
                printf("%.4lf\n",mid);
                flag=1;
                break;
            }
            else if(ans(mid)-y>1e-6)
            {
                r=mid;
            }
            else if(y-ans(mid)>1e-6)
            {
                l=mid;
            }
       }

        if(!flag)
        printf("No solution!\n");
    }
    return 0;    
}

以上是关于杭电2199.Can you solve this equation?的主要内容,如果未能解决你的问题,请参考以下文章

HDU 2199 Can you solve this equation?

HDU 2199 Can you solve this equation?(二分搜索)

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

hdu 2199Can you solve this equation?(牛顿迭代法)

杭电2266 How Many Equations Can You FindDFS

杭电2018多校第五场(2018 Multi-University Training Contest 5) 1007.Glad You Came (HDU6356)-区间更新-线段树+剪枝