当前位置:编程学习 > C/C++ >>

Triangle

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;
}

 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,