soj1209- 最短的距离(精度问题)
1209: 最短的距離
Time Limit: 1 Sec??Memory Limit: 128 MB Submit: 149??Solved: 5 [Submit][Status][Web Board]Description
如圖所示:
這里有一個平行于X軸的長方形(紅色標(biāo)出)和一個點(x,y);你能幫UnkelTao計算下點到長方形的最短距離嗎?
?
?
Input
多組測試數(shù)據(jù);
每組數(shù)據(jù)含有10個實數(shù),前八個分別表示長方形的四個坐標(biāo)x0,y0,x1,y1,x2,y2,x3,y3;后兩個表示點的坐標(biāo)x,y;??其中每個數(shù)的絕對值均小于10000并且矩陣的四個坐標(biāo)點均按順時針或者逆時針給出。
保證輸入圖形為矩陣。.
?
Output
輸出最短的距離值,結(jié)果保留2位小數(shù)。輸出格式見樣例
?
Sample Input
0 0 0 2 2 2 2 0 1 3 0 0 0 2 2 2 2 0 3 3Sample Output
1.00 1.41HINT
輸入輸出最好使用%lf
?
----------------------------------------------------
#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<fstream>
using namespace std;
double eps=0.00000001;
double x1,x2,yy1,y2;
double x,y;
double sqr(double a)
{
??? return a*a;
}
double minn(double a,double b,double c,double d)
{
??? if (a-b<eps) b=a;
??? if (b-c<eps) c=b;
??? if (c-d<eps) d=c;
??? return d;
}
double deal()
{
??? double t,t1,t2,t3,t4;
??????? if (x-x1<=eps)
??????? {? t1=x-x1;t2=x2-x;
??????? t
??????????? if (y-y2>=eps) return sqrt(sqr(x1-x)+sqr(y-y2));
??????????? if (y-yy1<=eps) return sqrt(sqr(x1-x)+sqr(yy1-y));
??????????? return x1-x+eps;
??????? }
??????? if (x-x2>=eps)
??????? {
??????????? if (y-y2>=eps) return sqrt(sqr(x2-x)+sqr(y-y2));
??????????? if (y-yy1<=eps) return sqrt(sqr(x2-x)+sqr(yy1-y));
??????????? return x-x2+eps;
??????? }
??????? if (y-y2>=eps) return (y-y2+eps);
??????? if (y-yy1<=eps) return (yy1-y+eps);
????? 3=y-yy1;t4=y2-y;
??????? return minn(t1,t2,t3,t4)+eps;
}
int main()
{
??? double t,a,b;
?? // freopen("data.in","r",stdin);
??//? freopen("a.out","w",stdout);
??? while (scanf("%lf%lf%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&yy1,&a,&b,&x2,&y2,&a,&b,&x,&y)!=EOF)
?????? {
?????????? if (x1-x2>=eps) {t=x1;x1=x2;x2=t;}
?????????? if (yy1-y2>=eps) {t=yy1;yy1=y2;y2=t;}
?????????? printf("%.2lf\n",deal());
?????? }
}
?
轉(zhuǎn)載于:https://www.cnblogs.com/mbcxm/p/3194729.html
總結(jié)
以上是生活随笔為你收集整理的soj1209- 最短的距离(精度问题)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jpg图片转eps 用于LaTeX
- 下一篇: SSH下载的方法