搜索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
- 搜索0之1001
- 搜索之线性搜索和二分搜索
- 英文单词搜索之词霸搜索
- 搜索之二分
- Win7搜索之体验!
- 人民搜索之我见
- Android之搜索框架
- 【搜索】魔鬼之城
- linux之搜索
- HDU1864之背包/搜索
- 搜索算法之总结
- 搜索是电商必争之家
- 百度地图之搜索
- 搜索之BM25 转
- hdu2208之搜索
- hdu2614之简单搜索
- hdu2616之简单搜索
- hdu2952之搜索
- 学习Android从0开始之ActionBar(活动条)
- [OJ]24点游戏之递归实现(JAVA)
- JavaScript形参和实参
- Jsonp学习记录
- 第七周项目一-三个函数在一个程序中(5)
- 搜索0之1001
- HDU 1024 Max Sum Plus Plus(求m个不相交连续子序列最大和/01背包)
- 你看不见的幕后推手
- String 的基本用法
- C\C++精华总结
- Android中实现从SD卡的Gallery画廊带回图片
- Android:Android面试准备二
- JavaScript中if的表达式自动转换为false的情况
- 关于启用sessionState外部存储,导致IHttpAsyncHandler长连接阻塞线程的问题