腾讯2018笔试模拟题之判断正方形

第一行输入n;
随后输入n组数据,每组数据的第一行先输入横坐标,第二行输入纵坐标;

判断四个点能否组成正方形,若是输出Yes,否则输出No.

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;


struct point
{
	int x,y;
} b[4];


bool cmp(point a, point b)
{
	if (a.x != b.x)
		return a.x < b.x;
	return a.y < b.y;
}


int TwoPointDistance(point a, point b)
{
	return sqrt(pow((a.x - b.x), 2) + pow((a.y - b.y), 2));
}


bool IsRightAngle(point a, point b, point c)
{
	int z;
	z = (a.x - b.x)*(a.x - c.x) + (a.y-b.y)*(a.y-c.y);
	if (z < 0.00001)
		return 1;
	else
		return 0;
}


int main() 
{
	int s1, s2, s3, s4;
	int t;
	cin >> t;
    int **a = new int* [2*t];
	for (int i = 0; i < 2 * t; i++)
		a[i] = new int[4];
	for(int i = 0; i < 2*t; i++)
	{
		for (int j = 0; j < 4; j++)
			cin >> a[i][j];
	}


	/*for (int i = 0; i < 2 * t; i++) {
		for (int j = 0; j < 4; j++)
			cout << a[i][j] << " ";
		cout << endl;
	}
	*/


	for (int i = 0; i < t; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			b[j].x = a[2 * i][j];
			b[j].y = a[2*i+1][j];
		}
		 
		// 确定点,进行排序,点进行标号
		sort(b, b + 4, cmp);


		// 确定边
		s1 = TwoPointDistance(b[0], b[2]);
		s2 = TwoPointDistance(b[0], b[1]);
		s3 = TwoPointDistance(b[1],b[3]);
		s4 = TwoPointDistance(b[2], b[3]);


		//分析是否为正方形
		if (s1 == s2&&s3 == s4&&s1 == s3&&s1 != 0 && IsRightAngle(b[0], b[1], b[2]))
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	
	//释放动态声明的数组
	for (int i = 0; i < 2 * t;i++)
		delete []a[i];
	delete []a;


	system("pause");


	return 0;
}


猜你喜欢

转载自blog.csdn.net/qinxinli/article/details/79705859