-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBase.java
47 lines (40 loc) · 859 Bytes
/
Base.java
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
package TestSample;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Base {
int base;
int q; // quotient
List<Integer> r = new ArrayList<Integer>(); // remainder
// default constructor
Base() {
base = 2;
}
// parameterized constructor
Base(int n) {
base = n;
}
// recursive loop
public void decToNBase(int n) {
q = n / base;
if (q == 0) {
r.add(n % base);
} else {
r.add(n % base);
decToNBase(q);
}
}
public List<Integer> revNBase() {
Collections.reverse(r);
return r;
}
public int convertToInt(List<Integer> list) {
String res = "";
for (int i = 0; i < list.size(); i++) {
res += list.get(i);
}
res = res.trim(); // remove whitespaces
int n = Integer.parseInt(res); // convert string to int
return n;
}
}