#include <iostream>
#include<stack>
using namespace std;
int main() {
int n, k;
stack<int> stk;
while (cin >> n >> k) {
if (n == 0) {
cout << '0' << endl;
continue;
}
if (n < 0) {
cout << '-';
n = -n;
}while (n) {
stk.push(n % k);
n = n / k;
}
while (stk.getsize() != 0) {
int x = stk.pop();
if (x >= 10) {
printf("%c", 'A' + x - 10);
}
else {
printf("%d", x);
}
}
cout << endl;
}
return 0;
}

总结:要将其转换为一个任意进制的数,只需要先用这个数去对 这个进制数取余,然后再将这个数除以这个进制数。取余的结果就是这个数转换为任意进制的数的结果。
如5转换为二进制数为101 ,4转换为二进制数为100。