以升序打印二维坐标点,并附上其在 C++ 中发生的频率
在此问题中,给出 2 个数组 x[] 和 y[],其中 (x,y) 给出了二维平面中某个点的坐标。我们的任务是打印所有点,พร้อม其出现的频率。
让我们通过一个例子来理解这个问题
Input: x[]={0, 1, 1, 0, 0} ; y[]={1, 2, 2, 2, 1}
Output
(0, 1) = 2
(1, 2) = 2
(0, 2) = 1若要解决此问题,我们需要存储每个点的出现频率。因此,我们需要使用 map 数据结构。该 map 的键是 (x[i], y[i]),映射值是出现的整数频率。
该程序将展示我们解决方案的实现情况,
示例
#include <bits/stdc++.h>
using namespace std;
void printFrequencyofPoint(int x[], int y[], int n){
map<pair<int, int>, int> pFreq;
for (int i = 0; i < n; i++)
pFreq[make_pair(x[i], y[i])]++;
map<pair<int, int>, int>::iterator i;
for (i = pFreq.begin(); i != pFreq.end(); i++) {
cout<<"("<<(i->first).first <<", "<< (i->first).second <<") -> ";
cout<<i->second << "\n";
}
}
int main() {
int x[]={0, 1, 1, 0, 0};
int y[]={1, 2, 2, 2, 1};
int n=5;
cout<<"The points and their frequency of occurance is :\n";
printFrequencyofPoint(x, y, n);
return 0;
}输出
The points and their frequency of occurance is : (0, 1) -> 2 (0, 2) -> 1 (1, 2) -> 2
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP