思路:先将十六进制数转化为二进制数,然后在将二进制数转化为八进制数,
一个十六进制数对应4个二进制数,一个八进制数对应3个二进制数。
自己这个方法有点麻烦QWQ
代码
#include#include using namespace std; int sum1[100000]; int main() { string num,num1,num2; int n,i,sum=0; cin >> n; while(n--) { num.clear(); num1.clear(); cin >> num; for(i=0;i 4) { case '0':num1 += ("0000");break; case '1':num1 += ("0001");break; case '2':num1 += ("0010");break; case '3':num1 += ("0011");break; case '4':num1 += ("0100");break; case '5':num1 += ("0101");break; case '6':num1 += ("0110");break; case '7':num1 += ("0111");break; case '8':num1 += ("1000");break; case '9':num1 += ("1001");break; case 'A':num1 += ("1010");break; case 'B':num1 += ("1011");break; case 'C':num1 += ("1100");break; case 'D':num1 += ("1101");break; case 'E':num1 += ("1110");break; case 'F':num1 += ("1111");break; } } if(num1.length() % 3 != 0) // 补前导0 { for(i=0;;i++) { num1.insert(0,"0"); if(num1.length() % 3 == 0) break; } } sum = 0; for(i=0;i 1) { if(num2[1] == '0') { if(num2[2] == '1') cout << 1; else if(i != 2) cout << 0; } else if(num2[1] == '1') { if(num2[2] == '0') cout << 2; else if(num2[2] == '1') cout << 3; } } else if(num2[0] == '1') { if(num2[1] == '0') { if(num2[2] == '1') cout << 5; else if(num2[2] == '0') cout << 4; } else if(num2[1] == '1') { if(num2[2] == '0') cout << 6; else if(num2[2] == '1') cout << 7; } } } } cout << endl; } }
Comments | NOTHING