HDU 5562 Clarke and five-pointed star (BC)

本文介绍了一种通过坐标判断五个点是否能构成正五角星的算法。提供了两种方法:一是将点按逆时针排序并检查边长;二是计算所有点间距离,对比边长与对角线长度。

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

Clarke and five-pointed star

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size:  

Problem Description

Clarke is a patient with multiple personality disorder. One day, Clarke turned into a learner of geometric. 
When he did a research with polygons, he found he has to judge if the polygon is a five-pointed star at many times. There are 5 points on a plane, he wants to know if a five-pointed star existed with 5 points given.

Input

The first line contains an integer T(1T10), the number of the test cases. 
For each test case, 5 lines follow. Each line contains 2 real numbers xi,yi(109xi,yi109), denoting the coordinate of this point.

Output

Two numbers are equal if and only if the difference between them is less than 104
For each test case, print Yes if they can compose a five-pointed star. Otherwise, print No. (If 5 points are the same, print Yes. )

Sample Input

2
3.0000000 0.0000000
0.9270509 2.8531695
0.9270509 -2.8531695
-2.4270509 1.7633557
-2.4270509 -1.7633557
3.0000000 1.0000000
0.9270509 2.8531695
0.9270509 -2.8531695
-2.4270509 1.7633557
-2.4270509 -1.7633557

Sample Output

Yes
No

Hint


Source

BestCoder Round #62 (div.2)

分析:给出5个点的坐标,判断是否是一个正五角星

方法一:判断是正五角星,只需要判断是正五边形,因为输入的点是随机输入的,所以先把点按逆时针排一下序,然后再计算五边形的边是否相等即可。

方法二:把每个点到其他各点的距离求出来,排序,小的五条边是五边形的边,大的五条边是五边形的对角线。判断小的边相等,大的边相等即可。

代码如下(排序):

#include <stdio.h>
#include <math.h>
typedef struct node{
	double x,y;
	double k;
}Node;
Node ans[10],left[10],right[10];
double sumx,sumy;
void sort()
{//点的排序 
	int i,j;
	sumx=sumx/5.0;
	sumy=sumy/5.0;
	int le,ri;
	le=ri=0;
	for(i=0;i<5;i++)
	{
		ans[i].k=(ans[i].y-sumy)/(ans[i].x-sumx);
		if(ans[i].x<=sumx)
			left[le++]=ans[i];
		else
			right[ri++]=ans[i];
	}
	for(i=0;i<le-1;i++)
	{
		for(j=i+1;j<le;j++)
		{
			if(left[i].k > left[j].k)
			{
				Node temp=left[i];
				left[i]=left[j];
				left[j]=temp;
			}
		}
	}
	for(i=0;i<ri-1;i++)
	{
		for(j=i+1;j<ri;j++)
		{
			if(right[i].k>right[j].k)
			{
				Node temp=right[i];
				right[i]=right[j];
				right[j]=temp;
			}
		}
	}
	int num=0;
	for(i=0;i<le;i++)
		ans[num++]=left[i];
	for(i=0;i<ri;i++)
		ans[num++]=right[i];
}

int main()
{
	int T;
	int i; 
	scanf("%d",&T);
	while(T--)
	{
		sumx=sumy=.0;
		for(i=0;i<5;i++)
		{
			scanf("%lf %lf",&ans[i].x,&ans[i].y);
			sumx+=ans[i].x;
			sumy+=ans[i].y;
		}
		sort();
		double avg = sqrt((ans[0].x-ans[1].x)*(ans[0].x-ans[1].x)+(ans[0].y-ans[1].y)*(ans[0].y-ans[1].y));
		int peace=0;
		for(i=1;i<4;i++)
		{
			double temp=sqrt((ans[i].x-ans[i+1].x)*(ans[i].x-ans[i+1].x)+(ans[i].y-ans[i+1].y)*(ans[i].y-ans[i+1].y));
			if(fabs(avg - temp)<=0.0001)
				continue;
			else{
				peace=1;
				break;
			}
		}
		if(!peace)
			printf("Yes\n");
		else
			printf("No\n");
	}
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值