南华大学暑假个人练习第一场 1006: Triangle

1006: Triangle

Time Limit: 1 Sec   Memory Limit: 32 MB
Submit: 30   Solved: 9
[ Submit][ Status][ Web Board]

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
 
 
这道题要我们判断所给的点中是否有3个点能构成等腰三角形,如果有则输出“YES”,不然输出“NO”。
思路:
这里我们可以用结构体来处理数据,但要注意的是输入的数据是实数,故要对数据定义成double类型的,在定义一个结构体数组,存储所输入的数据。
在后面比较两个点之间的距离是否相等的时候,以为实数之间存在一定的误差,故两个数之间不能直接划等号,应该两个数相减,与定义的一个数相比较,看是否小于所定义的数,如果小于,则表示相等,反之,不相等。
下面睡我的代码:

#include<iostream>
#include<cmath>
using namespace std;

struct S
{
    double x,y;
    double dist;
}str[501];

int main(void)
{
    int N;
    while(cin>>N)
    {
        int i,j,k;
        int flag=0;
        for(i=0; i<N; i++)
        {
            cin>>str[i].x>>str[i].y;
        }
        for(i=0; i<N; i++)
        {
            for(j=0; j<N; j++)
            {
                str[j].dist=(str[i].x-str[j].x)*(str[i].x-str[j].x)+(str[i].y-str[j].y)*(str[i].y-str[j].y);
            }
            for(j=0; j<N; j++)
            {
                for(k=j+1; k<N; k++)
                {
                    if(abs(str[j].dist-str[k].dist)<0.000001)
                    {
                        double x1=str[j].x-str[i].x;
                        double y1=str[j].y-str[i].y;
                        double x2=str[k].x-str[i].x;
                        double y2=str[k].y-str[i].y;
                        if(abs(x1*y2-x2*y1)>0.000001)
                        {
                            flag=1;
                            break;
                        }
                    }
                }
            }
        }
        if(flag)
            cout<<"YES"<<endl;
        else
            cout<<"NO"<<endl;
    }
    return 0;

}

 
 
 
 
 
 
 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值