C++编程用递归法将一个m进制的整数转换成n进制的整数,其中m,n为小于10的整数?
2020-04-01
程序实现把m进制的x转为n进制,递归实现
例如,7进制的542转为5进制为2100
#include<iostream>
typedef long long LL;
LL f(int n, int i, LL p) {
if (i == 0) return p;
else return f(n, i - 1, p * n);
}
LL m2ten(LL x, int m, int i, LL s) {
if (x == 0) return s;
else return m2ten(x / 10, m, i + 1, s + x % 10 * f(m, i, 1));
}
void ten2n(LL t, int n) {
if (t > n - 1)
ten2n(t / n, n);
std::cout << t % n;
}
void m2n(LL x, int m, int n) {
ten2n(m2ten(x, m, 0, 0), n);
}
int main() {
LL x;
int m, n;
std::cin >> x >> m >> n;
m2n(x, m, n);
std::cout<<std::endl;
return 0;
}