主要就是16->2然后2->8。
注意不要用函数写,不然会超时。
#include <iostream>
#include <cstring>
using namespace std;
int main()
{int n;cin>>n;while(n--){string x;cin>>x;string r1="";int i=0;for(i=0;i<x.length();i++){if(x[i]=='0') r1+="0000";else if(x[i]=='1') r1+= "0001";else if(x[i]=='2') r1+= "0010";else if(x[i]=='3') r1+= "0011";else if(x[i]=='4') r1+= "0100";else if(x[i]=='5') r1+= "0101";else if(x[i]=='6') r1+= "0110";else if(x[i]=='7') r1+= "0111";else if(x[i]=='8') r1+= "1000";else if(x[i]=='9') r1+= "1001";else if(x[i]=='A') r1+= "1010";else if(x[i]=='B') r1+= "1011";else if(x[i]=='C') r1+= "1100";else if(x[i]=='D') r1+= "1101";else if(x[i]=='E') r1+= "1110";else if(x[i]=='F') r1+= "1111";}if((r1.length()%3)==1) r1="00"+r1;else if((r1.length()%3)==2) r1="0"+r1;x=r1;int show=0;for(int i=0;i<x.length();i+=3){int y=((x[i]-'0')*4+(x[i+1]-'0')*2+(x[i+2]-'0')*1);if(y==0&&show==0) continue;cout<<y;show=1;}cout<<endl;}}