-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProductofDigits.java
51 lines (43 loc) · 1.13 KB
/
ProductofDigits.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
48
49
50
51
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class ProductofDigits
{
public static void main(String[] argv) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
long n = in.nextLong();
boolean f = true;
Stack<Integer> stack = new Stack<>();
if (n == 1) {
System.out.println(n);
return;
}
else if (n == 0) {
System.out.println("10");
return;
}
while(f) {
f = false;
for(int i = 9; i > 1; i--) {
if (n % i==0) {
stack.push(i);
n = (long)(n /(long) i);
// System.out.println("n="+n);
f=true;
break;
}
}
}
if (n > 1) {
System.out.println("-1");
return;
}
else {
while(stack.size()>0) {
System.out.print(stack.pop());
}
}
System.out.println("");
}
}