代码:
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 Format
The first line contains a integer TT indicating the total number of test cases.
Each test case begins with an integer nn, denoting the number of stars in the sky.
Following nn lines, each contains 22 integers x_i,y_ixi,yi, describe the coordinates of nn stars.
• 1 \le T \le 3001≤T≤300
• 3\le n\le 1003≤n≤100
• -10000 \le x_i, y_i \le 10000−10000≤xi,yi≤10000
• All coordinates are distinct.
Output Format
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).
样例输入复制
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
样例输出复制
NO YES NO
分析:
之前看过的题,直接就做了,因为题目说了坐标只能是整数,因此只能是正四边形;
代码:
#include<stdio.h>
#include<algorithm>
using namespace std;
int x[1005],y[1005];
int lenth[1005];
int main()
{
int t,n,i,j,k;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d %d",&x[i],&y[i]);
if(n!=4)
{
printf("NO\n");
continue;
}
k=0;
for(i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
lenth[k++]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
sort(lenth,lenth+6);//这里直接排序就好,最小的四个应该是相等的;
if(lenth[0]==lenth[1]&&lenth[0]==lenth[2]&&lenth[0]==lenth[3]&&lenth[4]==lenth[5]&&lenth[0]!=lenth[4])
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
博客围绕编写程序判断星星能否构成正多边形展开。将天空投影到二维平面,给出输入格式,包含测试用例数、星星数量及坐标等信息,输出格式为根据情况输出“YES”或“NO”。因坐标为整数,实际只能是正四边形。
32万+

被折叠的 条评论
为什么被折叠?



