-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathanybasemultiplication_2.java
More file actions
88 lines (55 loc) · 1.11 KB
/
anybasemultiplication_2.java
File metadata and controls
88 lines (55 loc) · 1.11 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import java.util.*;
public class anybasemultiplication_2{
public static void main(String args[]){
Scanner scn = new Scanner(System.in);
int b = scn.nextInt();
int n1 = scn.nextInt();
int n2 = scn.nextInt();
int d = getProduct(b, n1, n2);
System.out.println(d);
}
public static int getProduct(int b, int n1, int n2){
int rv = 0;
int p = 1;
while(n2 > 0){
int d2 = n2 % 10;
n2 /= 10;
int sprd = getProductWithASingleDigit(b, n1, d2);
rv = getSum(b, rv, sprd * p);
p *= 10;
}
return rv;
}
public static int getProductWithASingleDigit(int b, int n1, int d2){
int rv = 0;
int c = 0;
int p = 1;
while(n1 > 0 || c > 0){
int d1 = n1 % 10;
n1 = n1 / 10;
int d = d1*d2 + c;
c = d/b;
d = d%b;
rv = rv + p * d;
p = p *10;
}
return rv;
}
public static int getSum(int b, int n1, int n2){
int rv = 0;
int c = 0;
int p = 1;
while(n1 > 0 || n2 > 0 || c > 0){
int d1 = n1 % 10;
int d2 = n2 % 10;
n1 /= 10;
n2 /= 10;
int d = d1 + d2 + c;
c = d / b;
d = d % b;
rv += d * p;
p *= 10;
}
return rv;
}
}