题目链接: http://hihocoder.com/problemset/problem/1700
这道题也就是题意很不好理解,就是让#abcdef转换成两个字符相连的形式,比如#aabbcc,然后可以简化的输出#abc,题目问的就是找与#abcdef相离最近的#aabbcc这种形式。暴力枚举找最小值就好。
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#define oo 0x3f3f3f3f
using namespace std;
int chenge(char c){ // 转换成数字
if(c >= '0' && c <= '9'){
return c - '0';
}
else return c - 'a' + 10;
}
char chenge1(int x){ // 转换成16进制输出
if(x < 10)return x + '0';
else return x - 10 +'a';
}
int main()
{
string str;
cin>>str;
cout<<"#";
for(int i=1;i<7;i+=2){
int ans = chenge(str[i]) * 16 + chenge(str[i+1]);
int temp = oo;
int flag;
for(int i=0;i<16;i++){ // 枚举出最小情况
int temp1 = abs(ans - 17 * i);
if(temp > temp1){
temp = temp1;
flag = i;
}
}
cout<<chenge1(flag);
}
cout<<endl;
return 0;
}