某为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;
}