HDU-2199 二分搜索解方程
2013-07-17 07:58
330 查看
题目连接
分析:
该方程左边的明显就是在[0,100]区间单调递增的,这就适应了二分查找了。直接水过之。
#include<stdio.h>
#include<math.h>
#define eps 1e-10
int sign(double x)
{
return x<-eps?-1: x>eps;
}
double count(double x)
{
return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;
}
double bsearch(double y)
{
double i=0,j=100;
while(sign(j-i)>=0){
double mid=(i+j)/2;
double s=count(mid);
if(sign(s-y)>0)
j=mid-eps;
else
i=mid+eps;
}
return i;
}
int main()
{
int t;
double y,x;
scanf("%d",&t);
while(t--){
scanf("%lf",&y);
if(count(0)<=y&&y<=count(100))
printf("%.4lf\n",bsearch(y));
else
puts("No solution!");
}
return 0;
}
分析:
该方程左边的明显就是在[0,100]区间单调递增的,这就适应了二分查找了。直接水过之。
#include<stdio.h>
#include<math.h>
#define eps 1e-10
int sign(double x)
{
return x<-eps?-1: x>eps;
}
double count(double x)
{
return 8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6;
}
double bsearch(double y)
{
double i=0,j=100;
while(sign(j-i)>=0){
double mid=(i+j)/2;
double s=count(mid);
if(sign(s-y)>0)
j=mid-eps;
else
i=mid+eps;
}
return i;
}
int main()
{
int t;
double y,x;
scanf("%d",&t);
while(t--){
scanf("%lf",&y);
if(count(0)<=y&&y<=count(100))
printf("%.4lf\n",bsearch(y));
else
puts("No solution!");
}
return 0;
}
相关文章推荐
- hdu 2199:Can you solve this equation?(二分搜索)
- HDU-2199(二分搜索无限逼近)
- HDU 2199(搜索题,二分)
- NYOJ 503 & HDU 2199 解方程(二分)
- NYOJ 503 & HDU 2199 解方程(二分)
- HDU 2199(二分求方程解)
- hdu 2199 Can you solve this equation?(二分搜索)
- hdu 2199 Can you solve this equation?(二分查找方程的根)
- HDU 2199 Can you solve this equation? 二分求方程解
- HDU 2199 Can you solve this equation?(二分搜索)
- hdu2199 二分搜索
- HDU 2199 Can you solve this equation(二分水题)
- HDU 2289 Cup (二分搜索)
- HDU 2199 Can you solve this equation?(二分)
- HDU - 2199 Can you solve this equation? 二分 简单题
- hdu 2446 二分搜索解题报告
- HDU 2199 Can you solve this equation? (二分 水题)
- hdu 1795(欧拉函数+二分搜索)
- 【HDU】 5101 Select 二分搜索 upper_bound
- 简单搜索练习(一)二分入门 HDU 2141 Can you find it?POJ 2785 4 Value whose Sum is 0