Untitled
unknown
plain_text
a year ago
1.6 kB
8
Indexable
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
int N = 4; // Kích thước bản đồ N x N
int map[4][4] = {
{1, 2, 3, 4},
{2, 1, 4, 3},
{1, 3, 2, 4},
{4, 2, 1, 3}
};
map<int, vector<pair<int, int>>> mp;
// Duyệt qua các phần tử của bản đồ
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
int key = map[i][j];
int position = i * N + j;
// Tìm xem key đã có trong map hay chưa
auto it = mp.find(key);
if (it == mp.end()) {
// Nếu chưa có key, thêm cặp (1, position) vào vector
mp[key].push_back({1, position});
} else {
// Nếu đã có key, cập nhật tần suất và thêm vào vector
int frequency = it->second.back().first + 1;
mp[key].push_back({frequency, position});
}
}
}
// In ra key, tần suất và các vị trí tương ứng
for (auto it : mp) {
int key = it.first;
vector<pair<int, int>> freq_positions = it.second;
cout << "Key " << key << " xuất hiện " << freq_positions.size() << " lần tại các vị trí: ";
for (auto fp : freq_positions) {
int frequency = fp.first;
int position = fp.second;
int row = position / N;
int col = position % N;
cout << "[Tần suất: " << frequency << ", Vị trí: (" << row << ", " << col << ")] ";
}
cout << endl;
}
return 0;
}
Editor is loading...
Leave a Comment