搜索0之1001

来源:互联网 发布:sql注入绕过空格 编辑:程序博客网 时间:2024/05/22 17:11

1 题目编号:1001

2 题目内容:

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

3 解题思路形成过程:此题为简单的二分查找题,从0到100进行计算,令100为最大值,0为最小值,分别代入计算公式,分别得到一个数值,两值进行相减,若值小于1e-6,则结果为(max+min)/2,否则,可设一中间值为(max+min)/2,并判断其与y的大小,若比y大,则max=中间值,否则,最小值=中间值,然后,进行下一轮循环,直至满足条件为止

4 代码:

#include <iostream>
#include <cmath>
using namespace std;
double search(double x)
{
return 8 * pow(x, 4) + 7 * pow(x, 3) + 2 * pow(x, 2) + 3 * x + 6;
}
int main()
{
int n;
cin >> n;
cout.precision(4);
while (n--)
{
int y;
double first, last, mid;
cin >> y;
if (search(0) <= y && search(100) >= y)
{
first = 0;
last = 100;
while (last - first>1e-6)
{
mid = (first + last) / 2;
if (search(mid)>y)
last = mid;
else
first = mid;
}
cout << fixed << (last + last) / 2 << endl;
}
else
cout << "No solution!" << endl;
}
return 0;
}

1 0
原创粉丝点击