B - Can you solve this equation?
来源:互联网 发布:mysql增删改查 编辑:程序博客网 时间:2024/06/03 03:51
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!
题目很简单,直接用二分搜索法查找满足条件的x,排除y值可以取的范围,同时要注意结果的精度,四舍五入。
代码如下:
#include <iostream>#include <iomanip>#include <math.h>#include <algorithm>using namespace std;const double EPS = 1e-7;int f(double x, double y){ if(8 * pow(x,4) + 7 * pow(x,3) + 2 * pow(x,2) + 3 * x + 6 < y) return 1; return 0;}double bs(double y){ double lo = 0, hi = 100; double mi, ans=hi+1; while(lo + EPS < hi) { mi = (lo + hi) / 2.0; if(f(mi,y)) { lo = mi; if(ans == hi + 1) ans = mi; ans = max(ans,mi); } else { hi = mi; ans = min(ans,mi); } } if(ans == hi + 1) ans = -1; return ans;}int main(){ int t; double y; const long long max_num = 8 * pow(100,4) + 7 * pow(100,3) + 2 * pow(100,2) + 3 * 100 + 6; cin >> t; while(t--) { cin >> y; if(y > max_num || y < 6) { cout << "No solution!" << endl; } else { if(y == 6) { cout << fixed << setprecision(4) << 0.0000 << endl; continue; } if(y == max_num) { cout << fixed << setprecision(4) << 100.0000 << endl; continue; } double ans = bs(y); if(ans != -1) cout << fixed << setprecision(4) << (int)(ans * 10000 + 0.5) / 10000.0 << endl; else cout << "No solution!" << endl; } } return 0;}
0 0
- B - Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- 浅谈C语言中结构体的初始化
- hdu5742 It's All In The Mind(水)
- 南阳acm 57-6174问题
- 【杭电oj】1171 - Big Event in HDU(01背包)
- 【数据库】解释关系代数中的象集、除运算
- B - Can you solve this equation?
- 在VM中安装linux Red Hat7.0
- Notepad++ 快捷键整理
- Notification通知
- Yii2.0自带验证码使用心得分享 [ 2.0 版本 ]
- 贪心-HDU-5742-It's All In The Mind
- OC_模糊搜索(精华版)
- hdu5744 Keep On Movin(水)
- 如何制作.9png