代码
#include <iostream>
#include <vector>
#include <string>
using namespace std;
char Mapping[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', '-'
};
string converse(int digital, int weight) {
if (weight < 2 || weight > 16 || weight == 10) {
return "Are you sure weight is between 2 and 16 and not 10?";
}
int flag = 0;
if (digital < 0) {
flag = 1;
digital = -digital;
}
string str = "";
int divisible;
int remaining;
while (digital != 0) {
divisible = digital / weight;
remaining = digital - divisible * weight;
digital = divisible;
str = Mapping[remaining] + str;
}
if (flag) {
str = Mapping[15] + str;
}
return str;
}
int main() {
cout << converse(7, 2) << endl;
cout << converse(23, 12) << endl;
cout << converse(-4, 3) << endl;
return 0;
}
结果