题目链接:https://www.hpuoj.com/contest/16/problem/B/
二进制转十六进制,从后面开始,每4位为一个十六进制数,不足4位前面补0.
直接模拟
#include <iostream> #include <cstring> #include <cstdio> using namespace std; char str[100000+100]; char num[100]="0123456789abcdef"; void cal(int pos) { int res = 0; if (pos <= 0) {//小于等于4位 for (int i= 0;i<pos+4;i++) { res = res * 2 + (str[i] - '0'); } printf("%c",num[res]); return; } cal(pos-4); res = 0; for (int i = pos; i < pos + 4; i++) { res = res * 2 + (str[i] - '0'); } printf("%c",num[res]); } int main() { int t; cin >> t; while (t--) { cin >> str; int len = strlen(str); cal(len-4); printf("\n"); } return 0; }