使用C ++可以删除的最小点数,以便在轴的一侧获得剩余点。
问题陈述
在笛卡尔平面中给我们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
热门推荐
2 成都训练狗狗
3 怎么训练小狗狗
4 训练狗狗软件
5 如何训练狗狗拉屎
9 狗狗负重训练
10 狗狗多大可以训练大小便
11 怎么训练狗狗萨摩耶
12 狗狗训练书
13 鞍山狗狗训练
14 训练狗狗用品
15 怎样才能训练狗狗听话
16 狗狗如何训练大小便
17 怎样训练狗狗睡狗窝
18 训练狗狗工具