Time Limit: 1 Sec Memory Limit: 32 MB
Submit: 29 Solved: 8
Description
It is a simple task, for N points on the 2D plane, you are supposed to find whether there are three points which could form a isosceles triangle.
Input
There are several test cases. For each case, the first line is an integer N (3 <= N <= 500) indicating the number of points. N lines follow, each line contains two doubles(not exceed 1000.0), indicating the coordinates of the points.
Output
For each case, if there exists a solution which could form a isosceles triangle, output “YES”, else output “NO”.
Sample Input
3
0 0
1 1
-1 1
3
0 0
1 1
5 10
Sample Output
YES
NO
思路:題目大致意思是給你已知數的點,求出這些點之間是否能組成等腰三角形。
可以枚舉所有點,求出一點到其他所有點的距離,如果有兩個相等的距離並且沒在同一直線上,則組成等腰三角形。
代碼:
#include<iostream> #include<cstring> #include<stdio.h> #include<cmath> #include<algorithm> using namespace std; #define MAX 505 #define ERR 0.000001 struct Point { double x,y; }point[MAX]; double dis[MAX]; int main() { int n,i,j,k; while(scanf("%d",&n)!=EOF) { bool flag=false; for(i=1;i<=n;i++) scanf("%lf%lf",&point[i].x,&point[i].y); for(i=1;i<=n;i++) { memset(dis,0,sizeof(dis)); for(j=1;j<=n;j++) { dis[j]=sqrt(((point[i].x-point[j].x)*(point[i].x-point[j].x))+((point[i].y-point[j].y)*(point[i].y-point[j].y))); } for(j=1;j<=n;j++) { for(k=j+1;k<=n;k++) { if(fabs(dis[j]-dis[k])<ERR) { if((fabs((point[j].x-point[i].x)*(point[k].y-point[j].y)-(point[j].y-point[i].y)*(point[k].x-point[j].x))>ERR)) { printf("YES\n"); flag=true; goto result; } } } } } result: ; if(!flag) printf("NO\n"); } return 0; }