马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
//利用switch+string类型
//注意字符串要双引号
#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<math.h>
using namespace std;
int main(){
int n;
string str_temp,str_in;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
str_in="";
str_temp="";//给字符串赋空
cin>>str_in;
int len=str_in.length();
for(int j=0;j<len;j++){
switch(str_in[j]){
case '0': str_temp+="0000";break;
case '1': str_temp+="0001";break;
case '2': str_temp+="0010";break;
case '3': str_temp+="0011";break;
case '4': str_temp+="0100";break;
case '5': str_temp+="0101";break;
case '6': str_temp+="0110";break;
case '7': str_temp+="0111";break;
case '8': str_temp+="1000";break;
case '9': str_temp+="1001";break;
case 'A': str_temp+="1010";break;
case 'B': str_temp+="1011";break;
case 'C': str_temp+="1100";break;
case 'D': str_temp+="1101";break;
case 'E': str_temp+="1110";break;
case 'F': str_temp+="1111";break;
default: break;
}
}
//cout<<str_temp<<endl;
//把01串转化成目标串
int len_s=str_temp.length();
int ans=2;
int sum=0;
int r=len*4%3;
if(r==1){
if(str_temp[0]=='1')
printf("1");
}
else if(r==2){
if(str_temp[0]=='0'&&str_temp[1]=='1')
printf("1");
else if(str_temp[0]=='1'&&str_temp[1]=='0')
printf("2");
else if(str_temp[0]=='1'&&str_temp[1]=='1')
printf("3");
}
else{
if(str_temp[0]=='0'&&str_temp[1]=='0'&&str_temp[2]=='0')
r=3;
}
for(int i=r;i<len_s;i++)
{
if(str_temp[i]=='1')
sum+=pow(2,ans);
ans--;
if(ans==-1)
{
printf("%d",sum);
ans=2;
sum=0;
}
}
printf("\n");
}
}
从绿色字体部分往下就不明白了 求大佬解答! |