Files
python/GESP/Level4/GESP202409/未命名1.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

67 lines
1.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 256;
int b[N]; // 桶
int a[17], al;
string c[N];
int cl;
int get(char x) {
if (x >= '0' && x <= '9')
return x - '0';
return x - 'A' + 10;
}
int toDec(char a, char b) { return get(a) * 16 + get(b); }
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
c[++cl] = s; // 原始数组保存下来
// 字符串长度 s.size()
for (int j = 0; j < s.size(); j += 2) {
int idx = toDec(s[j], s[j + 1]); // 对应的十进制数值
b[idx]++; // 维护桶
}
}
// 猴子选大王共16次
for (int i = 1; i <= 16; i++) {
int mx = -1, mp = 0;
for (int j = 0; j < N; j++) {
if (b[j] > mx) {
mx = b[j];
mp = j;
}
}
a[++al] = mp; // 本轮次找到的大王位置是mp,记录下来到a数组中去
b[mp] = -1; // 避免重复选,把大王出列
}
// 遍历原始数组
for (int i = 1; i <= n; i++) {
for (int j = 0; j < c[i].size(); j += 2) {
int old = toDec(c[i][j], c[i][j + 1]);
// 需要与b数组中的每一个数值进行对比,看看与哪个数值的绝对值差最小,也就是与谁最接近`
int mi = INT_MAX,
mp = 0; // mi最小的差绝对值,mp就是最小差绝对值所在的位置号
for (int k = 1; k <= 16; k++) {
int x = b[k];
int d = abs(x - old);
if (d < mi) {
mp = k;
mi = d;
}
}
if (mp >= 10)
cout << mp - 10 + 'A';
else
cout << mp;
}
cout << endl;
}
return 0;
}