题目:
思路:
十进制数对16反复整除和求余,然后逆向输出即可。
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 4 char getnum(int n) 5 { 6 switch(n) 7 { 8 case 0: return '0'; break; 9 case 1: return '1'; break; 10 case 2: return '2'; break; 11 case 3: return '3'; break; 12 case 4: return '4'; break; 13 case 5: return '5'; break; 14 case 6: return '6'; break; 15 case 7: return '7'; break; 16 case 8: return '8'; break; 17 case 9: return '9'; break; 18 case 10: return 'A'; break; 19 case 11: return 'B'; break; 20 case 12: return 'C'; break; 21 case 13: return 'D'; break; 22 case 14: return 'E'; break; 23 case 15: return 'F'; break; 24 } 25 } 26 27 int main() 28 { 29 int a,num=0,k[15]; 30 cin>>a; 31 if(a==0) cout<<"0"; 32 while(a!=0) 33 { 34 k[num] = a%16; 35 num++; 36 a = a/16; 37 } 38 for(int i=num-1;i>=0;i--) 39 { 40 cout<<getnum(k[i]); 41 } 42 cout<<endl; 43 return 0; 44 }