题目链接:http://lx.lanqiao.cn/problem.page?gpid=T51
demo:
#include <iostream> #include <string> using namespace std; int main() { int Number; cin >> Number; for(int k = 1; k <= Number; k++) { string String_Hex, String_Binary; cin >> String_Hex; String_Binary = ""; for(int i = 0;i < String_Hex.length(); i++){ switch(String_Hex[i]) { case '0':String_Binary += "0000"; break; case '1':String_Binary += "0001"; break; case '2':String_Binary += "0010"; break; case '3':String_Binary += "0011"; break; case '4':String_Binary += "0100"; break; case '5':String_Binary += "0101"; break; case '6':String_Binary += "0110"; break; case '7':String_Binary += "0111"; break; case '8':String_Binary += "1000"; break; case '9':String_Binary += "1001"; break; case 'A':String_Binary += "1010"; break; case 'B':String_Binary += "1011"; break; case 'C':String_Binary += "1100"; break; case 'D':String_Binary += "1101"; break; case 'E':String_Binary += "1110"; break; case 'F':String_Binary += "1111"; break; default:break; } } if(String_Binary.length() % 3 == 1) String_Binary = "00" + String_Binary; else if(String_Binary.length() % 3 == 2) String_Binary = '0' + String_Binary; int Sign_Leading_Zero = 0; for(int i = 0;i <= String_Binary.length() - 3; i += 3){ int Digit_Number = 4 * (String_Binary[i] - '0') + 2 * (String_Binary[i+1] - '0') + (String_Binary[i+2] - '0'); if(Digit_Number) Sign_Leading_Zero = 1; if(Sign_Leading_Zero) cout << Digit_Number; } cout << endl; } return 0; }