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 rst line contains a integer T indicating the total number of test cases. Each test case begins with
an integer n, denoting the number of stars in the sky. Following n lines, each contains 2 integers xi, yi,
describe the coordinates of n stars.
• 1 ≤ T ≤ 300
• 3 ≤ n ≤ 100
• .10000 ≤ xi,yi ≤ 10000
• All coordinates are distinct.
Output
For each test case, 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即可。
AC代码
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
#include <cmath>
#include <map>
#include <list>
#define ll long long
using namespace std;
const int mod = 1e9+7;
const int maxn = 1e6+5;
struct point{ //用结构体数组表示点坐标
int x,y;
}p[105];
double dist (point a,point b){ //求距离函数
double d = (double)sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
return d;
}
double len[maxn];
int main()
{
int T;
scanf("%d",&T);
while (T--){
int n;
scanf("%d",&n);
for (int i = 0; i<n; i++){
scanf("%d%d",&p[i].x,&p[i].y);
}
int k = 0;
for (int i = 0; i<n; i++){
for (int j = i+1; j<n; j++){
len[k++] = dist(p[i],p[j]); //记录所有点之间距离
}
}
double Min = len[0];
for (int i = 0; i<k; i++){ //求出最小距离
if (Min>len[i]){
Min = len[i];
}
}
int cnt = 0;
for (int i = 0; i<k; i++){ //遍历数组求出等于最小距离的边数
if (len[i] == Min){
cnt++;
}
}
if (cnt == n){ //判断,输出
printf("YES\n");
}
else{
printf("NO\n");
}
}
return 0;
}