-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvertToBase10.java
More file actions
36 lines (31 loc) · 848 Bytes
/
Copy pathconvertToBase10.java
File metadata and controls
36 lines (31 loc) · 848 Bytes
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
public class convertToBase10{
public static void main(String[] args){
int result = convertToBase10(new int[] {1, 0, 1, 1}, 2);
System.out.println(result);
result = convertToBase10(new int[] {1, 1, 2}, 3);
System.out.println(result);
result = convertToBase10(new int[] {3, 2, 5}, 8);
System.out.println(result);
result = convertToBase10(new int[] {3, 7, 1}, 6);
System.out.println(result);
}
static int convertToBase10(int[] a, int base) {
int len = a.length;
int sum = 0;
if (isLegalNumber(a, base) == 0) return sum;
for (int i = 0; i < len; i++) {
sum += a[i] * Math.pow(base, len-1-i);
}
return sum;
}
static int isLegalNumber(int[] a, int base) {
int isLegal = 1;
for (int index = 0; index < a.length; index++) {
if (a[index] >= base) {
isLegal = 0;
break;
}
}
return isLegal;
}
}