判断正多边形

Problem Description

给出样例个数T,n个点及n个点的坐标xi,yi,判断这n个点能否构成正n边形。
若能够,输出“Yes.”,否则输出“No.”

Input

第一行为样例个数T,其后T组数据,每组数据以n开头。
每组数据包括n行表示n个点,每行两个整数xi,yi分别表示该点的x,y坐标。
1<=T<=100
1<=n<=100
1<=xi,yi<=1000

Output

若能够构成正n边形,输出“Yes.”,否则输出“No.”

Sample Input

3
3
0 0
1 0
1 1
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.
#include<iostream>
using namespace std;
bool judge(double ZX, double ZY,double x[],double y[],double n)
{
	double distance1 = (x[0] - ZX)*(x[0] - ZX) + (y[0] - ZY)*(y[0] - ZY);
	for (int i = 1; i < n; i++)
	{
		double distance2 = (x[i] - ZX)*(x[i] - ZX) + (y[i] - ZY)*(y[i] - ZY);
		if (distance1 != distance2)
			return false;
	}
	return true;
}
int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		double sum1 = 0, sum2 = 0;
		double *x = new double[n];
		double *y = new double[n];
		for (int i = 0; i < n; i++)
		{
			cin >> x[i] >> y[i];
			sum1 += x[i];
			sum2 += y[i];
		}
		double ZX, ZY;
		ZX = sum1 / n;
		ZY = sum2 / n;
		if (judge(ZX,ZY,x,y,n))
			cout << "Yes." << endl;
		else cout << "No." << endl;
	}
	return 0;
}


你可能感兴趣的:(判断正多边形)