canyon?最基本的二分查找
2016-04-18 13:23
330 查看
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.
[align=left]Input[/align]
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);
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2<br>100<br>-4<br>
[align=left]Sample Output[/align]
1.6152<br>No solution!<br>
#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
double f(double x)
{
double y = 8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6;
return y;
}
int main()
{
double y, left = 0.0,right = 100.0,mid,x;
int n;
scanf("%d",&n);
while(n--)
{
scanf("%lf",&y);
if(y<f(0)||y>f(100))
{
printf("No solution!\n");
return 0;
}
while((right - left)>1e-8)//二分查找的步骤代码,此处使精度
{
mid = (left + right)/2;
if(y>f(mid))
{
left = mid;
}
else
{
right = mid;
}
}
printf("%.4lf\n",mid);//保留4位小数
}
}
[align=left]Input[/align]
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);
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2<br>100<br>-4<br>
[align=left]Sample Output[/align]
1.6152<br>No solution!<br>
#include<iostream>
#include<cmath>
#include<stdio.h>
using namespace std;
double f(double x)
{
double y = 8*x*x*x*x + 7*x*x*x + 2*x*x + 3*x + 6;
return y;
}
int main()
{
double y, left = 0.0,right = 100.0,mid,x;
int n;
scanf("%d",&n);
while(n--)
{
scanf("%lf",&y);
if(y<f(0)||y>f(100))
{
printf("No solution!\n");
return 0;
}
while((right - left)>1e-8)//二分查找的步骤代码,此处使精度
{
mid = (left + right)/2;
if(y>f(mid))
{
left = mid;
}
else
{
right = mid;
}
}
printf("%.4lf\n",mid);//保留4位小数
}
}
相关文章推荐
- 百度地图步行箭头和路线一致
- aodh M版本新特性 - Remove eventlet from Aodh in favour of threaded approach
- OmniGraffle汉化破解版
- ngrok原理浅析
- 快速解决光标由竖线变黑块
- 【LeetCode】104. Maximum Depth of Binary Tree 求解二叉树最大深度
- 使用Mockito进行java单元测试
- SAP保存操作记录CDHDR和CDPOS表
- Android studio切换分支
- LinkedList基本用法
- static小解
- 关于C++构造函数一二
- 聊天(蘑菇街)-Java
- android adb, retrieve database using run-as
- 关于Unity3D5.X 动态批处理(Dynamic Batching)实测,区别于4.0版本
- [Android][Android Studio] *.jar 与 *.aar 的生成与*.aar导入项目方法
- 可以无限循环,自动旋转,停靠的3D旋转布局控件
- 通过CDHDR CDPOS获取增量数据
- 详解可变参数列表
- 个人开发安卓遇到的一些问题收集【仅解决个人问题】