思路:要使(ab - gh)^2 + (cd - ij)^2 + (ef - kl)^2,我们让每个值都最小即可。所以对于找到离ab最近的两位且相同十六进制数字即可。
stoi(字符串,起始位置,进制),将字符串转化为十进制数字。
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
const int MAXN = 105;
const int INF = 0x3f3f3f3f;
char work(int x)
{
string a = "0123456789abcdef";
int MIN = INF, pos;
for (int i = 0; i < a.size(); i++)
{
string str; str += a[i]; str += a[i];
int t = abs(x - stoi(str, 0, 16));
if (MIN > t) MIN = t, pos = i;
}
return a[pos];
}
int main()
{
//ios::sync_with_stdio(false);
string str;
while (cin >> str)
{
int a, b, c;
a = stoi(str.substr(1, 2), 0, 16);
b = stoi(str.substr(3, 2), 0, 16);
c = stoi(str.substr(5, 2), 0, 16);
cout << "#" << work(a) << work(b) << work(c) << endl;
}
return 0;
}
/*
#40e0d0
*/