Files
python/GESP/Level4/GESP202306/Test.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

102 lines
2.3 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];
string c[N]; // 原字符串数组
vector<int> v;
int toDec(string s) { // s是一个两位的十六进制字符串
int sum = 0;
if (s[0] >= 'A' && s[0] <= 'F')
sum = (s[0] - 'A' + 10) * 16;
else
sum = (s[0] - '0') * 16;
if (s[1] >= 'A' && s[1] <= 'F')
sum += s[1] - 'A' + 10;
else
sum += s[1] - '0';
return sum;
}
// 十进制转十六进制
string toHex(int x) {
string res;
while (x) {
int t = x % 16;
if (t >= 10)
res += t - 10 + 'A';
else
res += t + '0';
x /= 16;
}
reverse(res.begin(), res.end());
return res;
}
char find(string s) {
int x = toDec(s);
int mi = INT_MAX, mp = 0;
for (int i = 0; i < v.size(); i++) {
if (abs(x - v[i]) < mi) {
mi = abs(x - v[i]);
mp = i;
}
}
if (mp >= 10)
return (char)(mp - 10 + 'A');
return (char)(mp + '0');
}
int main() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
string s;
cin >> s;
c[i] = s; // 把原始数组记录下来,后面要用
for (int j = 0; j < s.size(); j += 2) {
char x = s[j], y = s[j + 1];
string t;
t.push_back(x);
t.push_back(y);
// 我要把两位字符串的十六进制数转为十进制
b[toDec(t)]++;
}
}
// 记录前十六名都有谁
// 我需要知道哪个灰阶颜色最多,前十六位都有谁?
for (int i = 1; i <= 16; i++) {
int mx = 0, mp = 0; // 每次都找最大值,并且记录最大值所在的pos位置
for (int j = 0; j < 256; j++) {
if (b[j] > mx) {
mx = b[j];
mp = j;
}
}
// 因为老大找出了,就不需要下次再参与评比了,否则它会一直是老大
b[mp] = 0;
v.push_back(mp); // 把前十六名都记录下来
}
for (int i = 0; i < v.size(); i++) {
string s = toHex(v[i]);
if (s.size() == 1)
s = "0" + s;
cout << s;
}
cout << endl;
for (int i = 1; i <= n; i++) {
string s = c[i];
for (int j = 0; j < s.size(); j += 2) {
char x = s[j], y = s[j + 1];
string t;
t.push_back(x);
t.push_back(y);
cout << find(t);
}
cout << endl;
}
return 0;
}