-
Notifications
You must be signed in to change notification settings - Fork 160
/
MbasetoNbase.cpp
61 lines (46 loc) · 1.14 KB
/
MbasetoNbase.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
Change a m base number to a n base number.
*/
#include<iostream>
#include<cassert>
#include<string>
using namespace std;
/*
solution1:
A very simple version.
m base number is first changed to a 10 base number, then the 10 base number is
change to a n base number.
Issue: the range is limited by the 10 base number, i.e., the int type.
O(n) time, O(1) space
*/
void MbaseToNbase1(char *numbasem, unsigned int m, char *numbasen, unsigned int n) {
assert(numbasem && m>0 && numbasen && n>0 );
int i = 0;
char c, *p = numbasen;
//change the m base number to a base 10 number
while (*numbasem != '\0')
i = i*m + *numbasem++ - '0';
while (i) {
*p++ = i % n + '0';
i /= n;
}
*p-- = '\0';
//reverse the n base number
while (p > numbasen) {
c = *p;
*p-- = *numbasen;
*numbasen++ = c;
}
}
/*
solution2:
a complex version which can process big number, we need to transfer
the m base nunber to n base number directly.
*/
int main() {
char *a = "10111";
char b[20] = {" "};
MbaseToNbase1(a, 2, b, 8);
cout<<b<<endl;
return 0;
}