二分查找——B:Can you solve this equation? 解题报告
来源:互联网 发布:windows xp的外观设置 编辑:程序博客网 时间:2024/06/05 16:25
<span style="font-size:18px;"></span>Can you solve this equation?
Description
现在,给出等式8* X^4+ 7* X^3+ 2* X^2+ 3 * X +6= Y,请找出他在0和100之间的解(包含0和100)。
现在,请你试试运气。。。。
Input
输入的第一行包含一个整数T(1 <= T <=100),表示测试用例的数目。接下来T个数字,每一行都有一个实数Y(abs(Y)<=10^10);
Output
对于每个测试用例,如果有解,你应该输出一个实数(精确到小数点后4位,四舍五入),如果在0到100之间无解,就输出“No solution!”。
Sample Input
2
100
-4
Sample Output
1.6152
No solution!
解题思路:容易看出方程左边是一个单调递增函数,解在1~100之间,如果 y 小于 f(0) 或大于 f(100) 则无解。排除无解后,在0~100中用二分查找 f(x)=y.
<span style="font-size:18px;">#include <iostream>#include <math.h>#include <cstdio>#include <cmath>using namespace std;const double EPS=1e-5;//精度double y;double f(double x){ return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;}double bs()//二分查找{ double lo=0,hi=100; double mi; while(1) { mi=(lo+hi)/2.0;//浮点数问题就别移位运算了 if(abs(f(mi)-y)<EPS) return mi; else if(y<f(mi)) hi=mi; else lo=mi; } return -1;}int main(){ int T; cin>>T; while(T--) { cin>>y; if(y<f(0)||y>f(100)) cout<<"No solution!"<<endl; else printf("%.4lf\n",bs()); } return 0;}</span>
0 0
- 二分查找——B:Can you solve this equation? 解题报告
- Can you solve this equation? (二分搜索)解题报告
- 【hdu2199】Can you solve this equation?——二分
- (step4.1)hdu2199(Can you solve this equation?——二分查找)
- acm解题报告 HDU 2199 Can you solve this equation?
- hdoj 2199 Can you solve this equation?【二分查找】【水】
- hdu 2199 Can you solve this equation? 简单二分查找
- HDU 2199 Can you solve this equation(二分查找)
- HDOJ 2199 Can you solve this equation?(二分查找法)
- 二分查找:hdu 2199 Can you solve this equation?
- HDOJ-2199-Can you solve this equation?(二分查找)
- HDOJ Can you solve this equation? 2199【二分查找】
- HDOJ 2199 Can you solve this equation?(二分查找)
- 二分查找 Problem 1001 Can you solve this equation?
- hdoj 2199 Can you solve this equation? 【二分查找】
- 1001—Can you solve this equation?
- Can you solve this equation?(二分)
- HDU2199:Can you solve this equation?(二分)
- 离散数学:构造性二难推理和破坏性二难定理的解释
- Android-百度地图之方向感应和模式更改
- 面向对象(方法的形式参数是类名的时候如何调用)
- js防刷新的倒计时代码(未测试)
- 即使是没有结果的爱恋,我依然会选择你
- 二分查找——B:Can you solve this equation? 解题报告
- CF 14 problem D - Two paths
- 离散数学中输出律的证明:(P∧Q→R)恒等于(P→(Q→R))
- POJ 1325 Machine Schedule 二分图 最小覆盖
- LeetCode 292. Nim Game
- CORS(跨域资源共享)简介。。。ie67不支持
- ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
- mysql导入导出 (未测试)
- iOS 使用AFN 进行单图和多图上传