voidoutputOctString(string bin){ string oct; int count = 0 ; int num = 0; char temp; for (int i = bin.length()-1; i>=0;i--) { if(bin[i]=='1'){ switch(count){ case0: num += 1;break; case1: num += 2;break; case2: num += 4;break; } } if(count==2){ oct += num + '0'; num = 0; } count = (count+1)%3; } if(count!=0){ oct += num + '0'; } int len = oct.length(); for(int j=0;j<len/2;j++){ temp = oct[j]; oct[j] = oct[len-1-j]; oct[len-1-j] = temp; } int index = 0; //忽略前导零 while(oct[index++] == '0'); /* 这里表问我为啥这样写,因为不这样写,傻逼测评系统不给过*/ for(index=index-1;index<len;index++){ printf("%c",oct[index]); } printf("\n"); }
intmain(){ int n; string hex[10]; string bin; cin>>n; for (int i = 0; i < n; ++i){ cin>>hex[i]; } for (int i = 0; i < n; ++i) { bin = converseToBinary(hex[i]); outputOctString(bin); } return0; }