ในปัญหานี้ เราได้รับ 2 อาร์เรย์ x[] , y[] ซึ่ง (x,y) ให้พิกัดของจุดในระนาบ 2 มิติ งานของเราคือการพิมพ์จุดทั้งหมดพร้อมกับความถี่ที่เกิดขึ้น
มาดูตัวอย่างทำความเข้าใจปัญหากัน
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