Re: 993 - Product of digits
Posted: Mon Feb 11, 2013 7:15 pm
Thanks CSGrandeur... you made me realize I was factorizing the digits in the wrong direction
, instead of 2 -> 9
![:)](./images/smilies/icon_smile.gif)
Code: Select all
20
0
1
2
7
9
10
48
96
18
100000000
7523475
643
156236
19
23
6746
59049
387420489
430467221
373248
Code: Select all
0
1
2
7
9
25
68
268
29
45555555588
-1
-1
-1
-1
-1
-1
99999
999999999
-1
888999
Code: Select all
import java.util.*;
import java.io.*;
public class P993
{
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws IOException
{
int numCases = Integer.parseInt(br.readLine());
for(int i=0; i<numCases; i++)
{
// int n = Integer.parseInt(br.readLine());
long n = Long.parseLong(br.readLine());
String ans = process(n);
// pw.println(process(n));
if(ans.equals("-1") || ans.length()==1)
pw.println(ans);
else
{
int prod = Integer.parseInt(ans.charAt(0)+"");
StringBuilder sb = new StringBuilder();
int last = 0;
for(int j=1; j<ans.length(); j++)
{
int d = Integer.parseInt(ans.charAt(j)+"");
if(prod * d >9)
{
sb.append(prod);
prod = d;
last = j;
}
else
prod*=d;
}
sb.append(ans.substring(last));
char[] c = sb.toString().toCharArray();
Arrays.sort(c);
pw.println(new String(c));
}
}
pw.close();
}
static String process(long n)
{
if(n>9)
{
for(int i=9; i>=2; i--)
{
if(n%i==0)
{
String s = process(n/i);
if(s.equals("-1"))
continue;
else
return i+""+process(n/i);
}
}
}
else
return n+"";
return "-1";
}
}