先轉十進制,再轉成題目要求的形式

 

#include <iostream>
#include <cstring>
#include <stack>

using namespace std;

int main()
{
    char str[1000];
    int base1, base2, tmp;
    long long int number_10;

    stack<char> output;

    while(cin >> str >> base1 >> base2){

        number_10 = 0; tmp = 1;

        for(int i = strlen(str) - 1; i>=0 ; i--){
            if(isdigit(str[i]))
                str[i] -= '0';
            else
                str[i] -= 'A' - 10;

            number_10 += str[i]*tmp;
            tmp *= base1;
        }

        while(number_10){
            tmp = number_10%base2;
            number_10 /= base2;
            if(tmp >= 10)
                tmp = tmp - 10 + 'A';
            else
                tmp += '0';

            output.push((char)tmp);
        }

        while(!output.empty()){
            cout << output.top();
            output.pop();
        }
        cout << endl;
    }
}
 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大神(偽) 的頭像
    大神(偽)

    大神的世界

    大神(偽) 發表在 痞客邦 留言(0) 人氣()