“蓝桥杯”练习系统 - 基础练习 - 十进制转十六进制

 

 1 #include <iostream>
 2 #include <stack>
 3 using namespace std;
 4 
 5 stack<int> s;
 6 char arr[16] {'0', '1', '2', '3', '4', '5', '6', '7',
 7               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
 8 int main()
 9 {
10     int a;
11     cin >> a;
12     
13     // 小心0的特殊情况
14     if (a == 0) {
15         cout << 0 << endl;
16         return 0;
17     }
18     
19     while (a) {
20         s.push(a % 16);
21         a /= 16;
22     }
23 
24     while (!s.empty()) {
25         cout << arr[s.top()];
26         s.pop();
27     }
28     cout << endl;
29 
30     return 0;
31 }

 

posted @ 2020-03-24 16:56  域Anton  阅读(128)  评论(0)    收藏  举报