进制转换 2-62

某为2017厦大提前批校招第四次笔试第三题:
输入三个数,分别为当前进制、目标进制,待转换整型。

建了两个表格用于转换。当时没考虑负数的情况,只过了62.5%。

#include <iostream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;

char out_arr[100];
int r_table[128];

void init(){
    //out_arr
    int i = 0;
    for(i = 0; i <10; i++)
    {
        out_arr[i] = '0' + i;
    }
    for(i = 0; i < 26; i++)
    {
        out_arr[i+10] = 'a' + i;
        out_arr[i+36] = 'A' + i;
    }

    //r_table
    for(i = 0; i < 10; i++)
    {
        r_table[i + '0'] = i;
    }

    for(i = 0; i < 26; i++)
    {
        r_table[i+'a'] = 10 + i;
        r_table[i+'A'] = 36 + i;
    }
}

int main()
{
    int a, b;
    string str;
    vector <int> result;


    init();

    while(cin >> a >> b >> str)
    {
        long long int value = 0;
        result.clear();

        if(str[0] == '-')
        {
            str = str.substr(1, str.size()-1);
            cout << '-';
        }

        //chage to normal 10 'jinzhi'.
        for(int i = 0; i < str.size(); i++){
            value = value * a + r_table[str[i]];
        }

        while(value)
        {
            result.push_back(value % b);
            value /= b;
        }

        for(int i = result.size() - 1; i >= 0; i--)
        {
            cout << out_arr[result[i]];
        }
        cout << endl;

    }//end of while(cin)

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值