使用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
热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便