思路:根据正方形的特性,四条边相等,两条对角线相等,所以只需要计算四点两两之间的距离,然后判断是否有且仅有两种长度的距离,就能判断这四个点是否能连成正方形。
C++代码如下:首先需要输入要测试的组数,然后依次输入两组数字,分别对应点的x坐标和y坐标,如果能连成正方形则输出“Yes”,否则输出“No”。
输入:
2
0 0 1 0
0 1 0 1
1 2 3 4
2 2 3 3
输出:
Yes
No
#include<iostream> #define LEN 6 using namespace std; bool inArray(int arr[], int num) { for (int i = 0; i<LEN; i++) { if (arr[i] == num) return true; } return false; } int main() { int t = 0; int x[4], y[4]; int len[6]; cin >> t; while (t>5 || t<1) { cin >> t; } int cnt; int length; while (t--) { cnt = 0; cin >> x[0] >> x[1] >> x[2] >> x[3]; cin >> y[0] >> y[1] >> y[2] >> y[3]; for (int i = 0; i < 4; i++) { for (int j = 1; j < (4 - i); j++) { length = (x[i] - x[i + j])*(x[i] - x[i + j]) + (y[i] - y[i + j])*(y[i] - y[i + j]); if (!inArray(len, length)) { len[cnt] = length; cnt++; } } } if (cnt == 2) cout << "Yes" << endl; else cout << "No" << endl; } return 0; }