原题传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2199
Can you solve this equation?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8116 Accepted Submission(s): 3738
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.
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!
Author
Redow
Recommend
分析:这道题目就是中学里学的二分法求方程的解的问题,注意符合要求的x,f(x) - Y<=0.000001,这个有精确度确定。
//二分法解方程 HDU 2199
#include<cstdio>
#include<cmath>
double low,mid,high;
int Y,t,flag;
using namespace std;
double f(double x){
double result = 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;
return result;
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&Y);
low = 0.0;
high = 100.0;
flag = 0;
while(low<high)
{
mid = (low+high)/2;
if(fabs(f(mid)-Y)<= 0.000001)
{
flag = 1;
break;
}else if(f(mid)-Y>0)
{
high = mid;
}else
{
low = mid;
}
}
if(flag){
printf("%.4f\n",mid);
}else{
printf("%s\n","No solution!");
}
}
}