题目: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;<br>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<br>100<br>-4<br>
Sample Output
1.6152<br>No solution!<br>
Author
Redow
题意:
二分法解一元方程
思路:
二分法解方程,注意 Y 和X 的范围,因为 范围的事错了好多
代码:
# include <iostream>
# include <cmath>
# include <iomanip>
using namespace std;
double work (double x)
{
return 8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6;
}
int main()
{
int n;
double m = work(100) ;
cin >> n;
while(n--)
{
int flag = 0;
double y;
cin >> y;
double ma = 100, mi = 0, mid;
while(1)
{
mid = (ma + mi)/2;
if (y < 26 || y > m)
{
cout << "No solution!"<<endl;
break;
}
//cout <<work(mid) << " " << mid << endl;
if (fabs(work(mid) - y) < 1e-4)
{
cout << fixed << setprecision(4) << mid <<endl;
break;
}
else if (work(mid) > y)
{
ma = mid;
}
else if (work(mid) < y)
{
mi = mid;
}
}
}
return 0;
}