文章标题 HDU 5533 : Dancing Stars on Me

本文介绍了一个有趣的问题:如何判断星星在夜空中是否能构成一个正多边形。通过计算每颗星星之间的距离,并找出是否存在恰好数量的最短边来实现这一目标。该方法使用了简单的几何原理和编程技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Dancing Stars on Me

Description

The sky was brushed clean by the wind and the stars were cold in a black sky. What a wonderful night. You observed that, sometimes the stars can form a regular polygon in the sky if we connect them properly. You want to record these moments by your smart camera. Of course, you cannot stay awake all night for capturing. So you decide to write a program running on the smart camera to check whether the stars can form a regular polygon and capture these moments automatically.

Formally, a regular polygon is a convex polygon whose angles are all equal and all its sides have the same length. The area of a regular polygon must be nonzero. We say the stars can form a regular polygon if they are exactly the vertices of some regular polygon. To simplify the problem, we project the sky to a two-dimensional plane here, and you just need to check whether the stars can form a regular polygon in this plane.

Input

The first line contains a integer indicating the total number of test cases. Each test case begins with an integer , denoting the number of stars in the sky. Following lines, each contains integers , describe the coordinates of stars.

All coordinates are distinct.

Output

For each test case, please output “YES” if the stars can form a regular polygon. Otherwise, output “NO” (both without quotes).

Sample Input

3
3
0 0
1 1
1 0
4
0 0
0 1
1 0
1 1
5
0 0
0 1
0 2
2 2
2 0

Sample Output

NO
YES
NO

题意: 给你n个点的坐标,看这n个点的坐标形成的图形是不是一个正n凸边形。
分析: 一开始以为这题很难,然后旁边的伙伴点破之后就就得是水体,其实,想让其形成正凸n边型,就得有n条边相等,且这n条边是所有两两的两个点的距离最短的。所以只要把n个点所有的两点之间的距离保存下来,看有没有n条最短的边。
代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue> 
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
struct node {
    int x,y;
};
node a[105];
int main ()
{
    int t;
    scanf ("%d",&t);
    while (t--){
        int n;
        scanf ("%d",&n);
        for (int i=0;i<n;i++){
            scanf ("%d%d",&a[i].x,&a[i].y);
        }
        vector <long long>v;//保存两点距离
        long long tmp;
        for (int i=0;i<n;i++){遍历所有的两点
            for (int j=i+1;j<n;j++){
                long long xn=a[i].x-a[j].x;
                long long yn=a[i].y-a[j].y;
                tmp=xn*xn+yn*yn;
                v.push_back(tmp);
            }
        }
        sort (v.begin(),v.end());//排序
        long long minx=v[0];
        int len=upper_bound (v.begin(),v.end(),minx)-v.begin();//找第一个比最短距离大的下标,即最短距离的条数。
        if (len==n){
            printf ("YES\n");
        }
        else printf ("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值