在C ++中按升序打印二维坐标点,然后打印其频率
在这个问题中,我们给了2个数组x[],y[],使得(x,y)给出2D平面中一个点的坐标。我们的任务是打印所有点及其出现的频率。
让我们以一个例子来了解问题
Input: x[]={0, 1, 1, 0, 0} ; y[]={1, 2, 2, 2, 1}
Output
(0, 1) = 2
(1, 2) = 2
(0, 2) = 1为了解决这个问题,我们需要存储每个点的出现频率。因此,我们需要使用映射数据结构。映射的关键是(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