以升序列印二維座標點,並附上其在 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