2014辽宁ACM省赛 Distance
2014-05-22 23:19
190 查看
问题 E: Distance
时间限制: 1 Sec 内存限制: 128 MB
提交: 48 解决: 12
[提交][状态][论坛]
题目描述
There is a battle field. It is a square with the side length 100 miles, and unfortunately we have two comrades who get hurt still in the battle field. They are in different positions. You have to save them. Now I give you the positions of them, and you should choose a straight way and drive a car to get them. Of course you should cross the battle field, since it is dangerous, you want to leave it as quickly as you can!
输入
There are many test cases. Each test case contains four floating number, indicating the two comrades' positions (x1,y1), (x2,y2).
Proceed to the end of file.
输出
you should output the mileage which you drive in the battle field. The result should be accurate up to 2 decimals.
样例输入
1.0 2.0 3.0 4.0 15.0 23.0 46.5 7.0
样例输出
140.01 67.61
提示
The battle field is a square local at (0,0),(0,100),(100,0),(100,100).
一道几何题,求出 边界上的两点 (x0,y0),(xn,yn)就可以了,不过有一些特殊情况得讨论,比如分母为0。
时间限制: 1 Sec 内存限制: 128 MB
提交: 48 解决: 12
[提交][状态][论坛]
题目描述
There is a battle field. It is a square with the side length 100 miles, and unfortunately we have two comrades who get hurt still in the battle field. They are in different positions. You have to save them. Now I give you the positions of them, and you should choose a straight way and drive a car to get them. Of course you should cross the battle field, since it is dangerous, you want to leave it as quickly as you can!
输入
There are many test cases. Each test case contains four floating number, indicating the two comrades' positions (x1,y1), (x2,y2).
Proceed to the end of file.
输出
you should output the mileage which you drive in the battle field. The result should be accurate up to 2 decimals.
样例输入
1.0 2.0 3.0 4.0 15.0 23.0 46.5 7.0
样例输出
140.01 67.61
提示
The battle field is a square local at (0,0),(0,100),(100,0),(100,100).
一道几何题,求出 边界上的两点 (x0,y0),(xn,yn)就可以了,不过有一些特殊情况得讨论,比如分母为0。
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<cmath> using namespace std; double distance(double x0,double y0,double xn,double yn) { return sqrt((x0-xn)*(x0-xn)+(y0-yn)*(y0-yn)); } double f1(double x,double x1,double y1,double x2,double y2) { return (y1-y2)/(x1-x2)*(x-x2)+y2; } double f2(double y,double x1,double y1,double x2,double y2) { return (x1-x2)/(y1-y2)*(y-y2)+x2; } int main() { double x1,y1; double x2,y2; while(cin>>x1>>y1>>x2>>y2) { double x0,y0,xn,yn; if(y1==y2||x1==x2) { printf("100.00\n"); continue; } x0=0; xn=100; y0=f1(x0,x1,y1,x2,y2); yn=f1(xn,x1,y1,x2,y2); // cout<<y0<<" "<<yn<<endl; if(yn>100&&y0>=0&&y0<=100) { yn=100; xn=f2(yn,x1,y1,x2,y2); } else if(yn>100&&y0<0) { yn=100; xn=f2(yn,x1,y1,x2,y2); y0=0; x0=f2(y0,x1,y1,x2,y2); } else if(y0<0&&yn>=0&&yn<=100) { y0=0; x0=f2(y0,x1,y1,x2,y2); } else if(y0>100&&yn>=0&&yn<=100) { y0=100; x0=f2(y0,x1,y1,x2,y2); } else if(y0>100&&yn<0) { y0=100; x0=f2(y0,x1,y1,x2,y2); yn=0; xn=f2(yn,x1,y1,x2,y2); } else if(y0>=0&&y0<=100&&yn<0) { yn=0; xn=f2(yn,x1,y1,x2,y2); } printf("%.2lf\n",distance(x0,y0,xn,yn)); } return 0; }
相关文章推荐
- 2014 辽宁 ACM 省赛 ( kuangbin oj 1020)|| Distance(两点式直线公式或直线求交点
- 2014辽宁ACM省赛 Prime Factors
- 2014辽宁ACM省赛 Lucky Numbers
- 2014辽宁ACM省赛 Prime Factors
- 2014辽宁ACM省赛 Guess
- 2014-2015 ACM-ICPC, Asia Xian Regional Contest G
- 2014 ACM/ICPC Asia Regional Guangzhou Online
- 2014 ACM/ICPC Asia Regional Guangzhou Online HDU 5024 Wang Xifeng's Little Plot
- 2014-2015 ACM-ICPC, NEERC, Moscow Subregional Contest C. CIA Datacenter
- HDU 5047 Sawtooth 规律+ C++大数模拟 2014 ACM/ICPC Asia Regional Shanghai Online
- hdu 5050 Divided Land---2014acm上海赛区网络赛
- 2014 ACM/ICPC 牡丹江赛区网络赛解题报告汇总
- ACM学习历程—Rotate(HDU 2014 Anshan网赛)(几何)
- 2014ACM/ICPC亚洲区域赛牡丹江站现场赛-I ( ZOJ 3827 ) Information Entropy
- zoj3822 Domination (The 2014 ACM-ICPC Asia Mudanjiang Regional Contest D题)概率dp
- XTU OJ 1209 Alice and Bob 2014(嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛)
- ACM学习历程——ZOJ 3822 Domination (2014牡丹江区域赛 D题)(概率,数学递推)
- hdu 5014 Number Sequence 2014 ACM/ICPC Asia Regional Xi'an Online
- 2014ACM亚洲区域北京邀请赛总结
- 2014-2015 ACM-ICPC, NEERC, Southern Subregional Contest (Online Mirror, ACM-ICPC Rules, Teams Preferred)