使用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
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP