题目大意:略。
解题思路:先整合16 To 2进制,注意补位为3的倍数(8进制),然后循环 i+=3 来跑完,前导0省略。
AC 代码
#include<bits/stdc++.h> #include<cmath> #define mem(a,b) memset(a,b,sizeof a); using namespace std; typedef long long ll; const int mlen=100000+100; char a[mlen]; int main() { // const string letrr[6]={"1010","1011","1100","1101","1110","1111"}; // const string numrr[10]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001"}; int T; scanf("%d",&T); T>10?T=10:T; while(T-- && ~scanf("%s",a)) { string txt; int len=strlen(a); int t=len*4%3,tlen=0; if(t==0) txt=""; else if(t==1) txt="00",tlen=2; else if(t==2) txt="0",tlen=1; for(int i=0;i<len;i++) // 这里不要用 a[i]-'0' 操作,会TLE,因为数据量太大 { switch(a[i]) { case '0':txt+="0000";break; case '1':txt+="0001";break; case '2':txt+="0010";break; case '3':txt+="0011";break; case '4':txt+="0100";break; case '5':txt+="0101";break; case '6':txt+="0110";break; case '7':txt+="0111";break; case '8':txt+="1000";break; case '9':txt+="1001";break; case 'A':txt+="1010";break; case 'B':txt+="1011";break; case 'C':txt+="1100";break; case 'D':txt+="1101";break; case 'E':txt+="1110";break; case 'F':txt+="1111";break; } } len=len*4+tlen; for(int i=0;i<len;i+=3) { if(txt[i]=='0'&&txt[i+1]=='0'&&txt[i+2]=='0') { if(i==0) continue; else printf("0"); } else if(txt[i]=='0'&&txt[i+1]=='0'&&txt[i+2]=='1') printf("1"); else if(txt[i]=='0'&&txt[i+1]=='1'&&txt[i+2]=='0') printf("2"); else if(txt[i]=='0'&&txt[i+1]=='1'&&txt[i+2]=='1') printf("3"); else if(txt[i]=='1'&&txt[i+1]=='0'&&txt[i+2]=='0') printf("4"); else if(txt[i]=='1'&&txt[i+1]=='0'&&txt[i+2]=='1') printf("5"); else if(txt[i]=='1'&&txt[i+1]=='1'&&txt[i+2]=='0') printf("6"); else if(txt[i]=='1'&&txt[i+1]=='1'&&txt[i+2]=='1') printf("7"); } puts(""); } return 0; }