Goat in the Garden 2&&http://acm.timus.ru/problem.aspx?space=1&num=1348
来源:互联网 发布:拿破仑战争 军粮 知乎 编辑:程序博客网 时间:2024/04/23 18:55
题意:给你一个山羊,问你山羊能吃到草需要拉伸的最小距离,和山羊能吃完草所要拉伸的最小距离
思路:首先求一点到一条线段距离要考虑垂线是否落在线段上,如果落在线段上,利用海伦公式求出高h即可,如果不在则求出该点到两个端点最短距离即可。注意a和b重合时要单独考虑。
AC代码:
#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>using namespace std;typedef struct{double x;double y;}Node;Node s[3];double dis(Node a,Node b){return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}int L;int main(){scanf("%lf%lf%lf%lf%lf%lf%d",&s[0].x,&s[0].y,&s[1].x,&s[1].y,&s[2].x,&s[2].y,&L);double a=dis(s[0],s[1]);double b=dis(s[0],s[2]);double c=dis(s[1],s[2]);double m1,m2;double s1=(a*a+b*b-c*c)/(2*a*b);double s2=(a*a+c*c-b*b)/(2*a*c);if(a==b+c) m1=0;//c点在直线上else if(s1<0||s2<0||a==0) m1=min(b,c);//高在直线外,要考虑a和b重合时else//高在直线上时,由海伦公式可得{ double p=(a+b+c)/2; double s=sqrt(p*(p-a)*(p-b)*(p-c)); double h=2*s/a; m1=h;}m2=max(b,c);m1-=L;m2-=L;printf("%.2lf\n%.2lf\n",m1>0?m1:0,m2>0?m2:0);return 0;}
- Goat in the Garden 2&&http://acm.timus.ru/problem.aspx?space=1&num=1348
- http://acm.timus.ru/problem.aspx?space=1&num=1416&&次小生成树
- http://acm.timus.ru/problem.aspx?space=1&num=1018&&Binary Apple Tree
- timus 1348. Goat in the Garden 2 URAL 点到线段的距离
- Goat in the Garden (ural 1348)
- ural 1348. Goat in the Garden 2
- Ural1084. Goat in the Garden
- timus 1084. Goat in the Garden URAL 解题报告 计算几何
- URAL 1348 Goat in the Garden 2(点到线段的距离)
- URAL 1348 Goat in the Garden 2计算几何(解题报告)
- sgu 1348 Goat in the Garden 2【点到线段的距离】
- URAL 1348 Goat in the Garden 2(点到线段的距离)
- 【点到线段距离】URAL - 1348 Goat in the Garden 2
- URAL 1348 Goat in the Garden 2(点到线段的距离)
- ural 1084. Goat in the Garden
- ural 1084 Goat in the Garden
- ural 1084. Goat in the Garden math
- 乌拉尔大学(URAL):http://acm.timus.ru/
- easyUI Combo
- 【PB】PB程序死锁问题及预防
- POJ 3468 A Simple Problem with Integers(线段树成段更新)
- new 与 alloc/init的区别
- Google的Objective-C编码规范
- Goat in the Garden 2&&http://acm.timus.ru/problem.aspx?space=1&num=1348
- android蓝牙开发
- 匹配合法email的正则式
- 【摄影笔记一】曝光参数
- HBase安装与配置
- c++ cout<<*p<<endl<<*p++;被重载导致输出错误问题原因!
- 【摄影笔记二】测光
- Apache编译安装php错误 libphp5.so : No such file or directory apxs:Error: Command failed with rc=65536
- MUBlockDelegate——一个基于Block的通用委托实现