HDOJ 2031 进制转换

来源:互联网 发布:思加图淘宝旗舰店真假 编辑:程序博客网 时间:2024/06/04 18:43

        进制转换,10进制转换成16进制。循环求余,余的逆序就是我们要的结果,当然不能直接输出,还必须输出对应的字符(10-->A, 11-->B等)。

#include <iostream>#include <fstream>#include <vector>using namespace std;const int MAX_SIZE = 32 + 2;char BASE[] = {'0', '1', '2', '3', '4', '5','6','7','8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};//#define yangyuanint main(){#ifdef yangyuan    ifstream in("in.txt");    cin.rdbuf(in.rdbuf());#endif // yangyuanvector<int> data;    data.reserve(MAX_SIZE);    int n, base;    while (cin >> n >> base){        bool positive = true;        if (n < 0)        {        positive = false;            n = -n;        }        do{            data.push_back(n % base);} while (n /= base);if (! positive)            cout << "-";        for (int i = data.size() - 1; i >= 0; i--){cout << BASE[data[i]];}cout << endl;        data.resize(0);}    return 0;}


原创粉丝点击