Cho tọa độ 3 điểm trên trang giấy. Nhiệm vụ: kiểm tra có thể xoay trang một góc nào đó không.
Xoay sao cho vị trí mới của x = vị trí cũ của y, vị trí mới của y = vị trí cũ của z.
Ví dụ:
Đầu vào: x = (0, 1), y = (1, 0), z = (0, -1)
Đầu ra: Yes
Giải thích: Có thể xoay 90°.
Phương pháp giải
Điều kiện xoay được:
- Khoảng cách x-y = khoảng cách y-z.
- 3 điểm không thẳng hàng (y không phải trung điểm của x và z).
Chương trình minh họa
Ví dụ
#include <bits/stdc++.h>
using namespace std;
int possibleOrNot(int coordinates[3][2]) {
long long dis1 = pow(coordinates[1][0] - coordinates[0][0], 2) + pow(coordinates[1][1] - coordinates[0][1], 2);
long long dis2 = pow(coordinates[2][0] - coordinates[1][0], 2) + pow(coordinates[2][1] - coordinates[1][1], 2);
if (dis1 != dis2)
return 0;
else if (coordinates[1][0] == ((coordinates[0][0] + coordinates[2][0]) / 2.0) && coordinates[1][1] == ((coordinates[0][1] + coordinates[2][1]) / 2.0))
return 0;
else
return 1;
}
int main() {
int coordinates[3][2] = {{0, 1}, {1, 0}, {0, -1}};
if (possibleOrNot(coordinates))
cout << "The rotation of page is possible";
else
cout << "The rotation of page is not possible";
return 0;
}
Kết quả
The rotation of page is possible