数据太大,用循环超时,看了别人的答案豁然开朗,自己重写了一遍。
#include<iostream>
#include<map>
#include<ctype.h>
#include<string>
using namespace std;
int main(int argc, char **argv) {
string str_16[]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100",
"1101","1110","1111"};
map<string,string>map_8;
map_8["000"]="0";
map_8["001"]="1";
map_8["010"]="2";
map_8["011"]="3";
map_8["100"]="4";
map_8["101"]="5";
map_8["110"]="6";
map_8["111"]="7";
int n;
cin>>n;
while(n--){
string str;
string str_2="";
string str_10="";
cin>>str;
for(int i=0;i<str.length();i++)
{
if(isalpha(str[i])){
str_2+=str_16[str[i]-'A'+10];
}
else if(isdigit(str[i])){
str_2+=str_16[str[i]-'0'];
}
}
if(str_2.length()%3==1)
str_2="00"+str_2;
if(str_2.length()%3==2)
str_2="0"+str_2;
string re="";
for(int i=0;i<str_2.length();i+=3){
string t=str_2.substr(i,3);
re+=map_8[t];
}
string::iterator p=re.begin();
while(*p++=='0');
--p;
while(p!=re.end()){
cout<<*p;
p++;
}
cout<<endl;
}
return 0;
}