三分查找,汽车转弯
2016-04-17 21:10
190 查看
Mr. West bought a new car! So he is travelling around the city.<br><br>One day he comes to a vertical corner. The street he is currently in has a width x, the street he wants to turn to has a width y. The car has a length l and a
width d.<br><br>Can Mr. West go across the corner?<br><img src=../../../data/images/2438-1.jpg><br>
[align=left]Input[/align]
Every line has four real numbers, x, y, l and w.<br>Proceed to the end of file.<br>
[align=left]Output[/align]
If he can go across the corner, print "yes". Print "no" otherwise.<br>
[align=left]Sample Input[/align]
10 6 13.5 4<br>10 6 14.5 4<br>
[align=left]Sample Output[/align]
yes<br>no<br>
如下图
本次的变量是α角,函数为h,h是先增大后减小的。
根据三分查找遍的:
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
double pi = acos(-1.0);//α角为180度
double s,h,x,y,l,w;//用于函数全局变量
double f(double p)//函数
{
s = l*cos(p) + w*sin(p) - x;
h = s*tan(p) + w*cos(p);
return h;
}
int main()
{
double left,right,mid,midmid;
while(scanf("%lf%lf%lf%lf",&x,&y,&l,&w)!=EOF)
{
left = 0.0;
right = pi/2;
while(fabs(right-left)>1e-8)//三分查找的终止条件,left与right的无限接近
{
mid = (left + right)/2;
midmid = (mid + right)/2;
if(f(mid) >= f(midmid))
right = midmid;
else
left = mid;
}
if(f(mid) <= y)
printf("yes\n");
else
printf("no\n");
}
}
width d.<br><br>Can Mr. West go across the corner?<br><img src=../../../data/images/2438-1.jpg><br>
[align=left]Input[/align]
Every line has four real numbers, x, y, l and w.<br>Proceed to the end of file.<br>
[align=left]Output[/align]
If he can go across the corner, print "yes". Print "no" otherwise.<br>
[align=left]Sample Input[/align]
10 6 13.5 4<br>10 6 14.5 4<br>
[align=left]Sample Output[/align]
yes<br>no<br>
如下图
本次的变量是α角,函数为h,h是先增大后减小的。
根据三分查找遍的:
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
double pi = acos(-1.0);//α角为180度
double s,h,x,y,l,w;//用于函数全局变量
double f(double p)//函数
{
s = l*cos(p) + w*sin(p) - x;
h = s*tan(p) + w*cos(p);
return h;
}
int main()
{
double left,right,mid,midmid;
while(scanf("%lf%lf%lf%lf",&x,&y,&l,&w)!=EOF)
{
left = 0.0;
right = pi/2;
while(fabs(right-left)>1e-8)//三分查找的终止条件,left与right的无限接近
{
mid = (left + right)/2;
midmid = (mid + right)/2;
if(f(mid) >= f(midmid))
right = midmid;
else
left = mid;
}
if(f(mid) <= y)
printf("yes\n");
else
printf("no\n");
}
}
相关文章推荐
- Java基础篇(关于this的一些用法)
- Educational Codeforces Round 9 C - The Smallest String Concatenation ,学习到string
- 明明的随机数之机试
- 20145223《Java程序程序设计》第7周学习总结
- 20159320《网络攻防实践》第7周学习总结
- 20159320《网络攻防实践》第7周视频总结
- 20159320《网络攻防实践》第7周教材总结
- mongodb入门很简单(2)
- PHP的输出缓冲区(转)
- JSONP浅析
- nginx负载均衡器处理session共享的几种方法(转)
- SQL Server安装过程+评估期已过+附加数据库5120错误的解决办法
- kafka中文教程
- 广东工业大学网络赛E题 思维题
- 理解AOP
- ****一步步构建大型网站架构
- Nginx安装部署
- STM32——ADC
- iOS开发常用术语————API,SDK,IDE和Xcode概念和关系
- POJ 3114 Tarjan+Dijkstra