在笛卡尔平面中给我们N点。我们的任务是找到要删除的最小点数,以使剩余点位于任何轴的一侧。
如果给定输入为{(10,5),(-2,-5),(13,8),(-14,7)},则如果我们删除(-2,-5),则所有剩余点都在X之上-轴。
因此答案是1。
1. Finds the number of points on all sides of the X-axis and Y-axis 2. Return minimum from both of them
#include <iostream> #include <algorithm> #define SIZE(arr) (sizeof(arr) / sizeof(arr[0])) using namespace std; struct point{ int x, y; }; int minPointsToBeRemoved(point arr[], int n){ int a = 0, b = 0, c = 0, d = 0; for (int i = 0; i < n; i++){ if (arr[i].x >= 0) b++; else if (arr[i].x <= 0) a++; if (arr[i].y <= 0) d++; else if (arr[i].y >= 0) c++; } return min({a, d, c, b}); } int main(){ point arr[] = {{10, 5}, {-2, -5}, {13, 8}, {-14, 7}}; cout << "Minimum points to be removed = " << minPointsToBeRemoved(arr, SIZE(arr)) << endl; return 0; }
输出结果
当您编译并执行上述程序时。它生成以下输出-
Minimum points to be removed = 1