进制转换(十进制整数转换成其他进制)

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 
 5 void convert(vector<char>&s, int n, int base) {
 6     static char digit[] =
 7         { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
 8     while (n > 0) {
 9         int reminder = n % base;
10         s.push_back(digit[reminder]);
11         n /= base;
12     }
13 }
14 
15 int main() {
16     vector<char> s;
17     int n, base;  //要转换的十进制整数和转换后的进制
18     
19     cin >> n >> base; 
20     convert(s, n, base);
21     for (int i = 0; i < s.size(); i++)
22         cout << s[s.size()-1-i];
23 }

 

posted on 2018-06-12 15:50  Pink.Pig  阅读(778)  评论(0编辑  收藏  举报