Page 2 of 2

Re: 993 - Product of digits

Posted: Mon Feb 11, 2013 7:15 pm
by mgavin2
Thanks CSGrandeur... you made me realize I was factorizing the digits in the wrong direction :), instead of 2 -> 9

Re: 993 - Product of digits

Posted: Wed Apr 03, 2013 5:08 pm
by alimbubt
Nice Problem.... :)
Sample Input and Output:
Input:

Code: Select all

20
0
1
2
7
9
10
48
96
18
100000000
7523475
643
156236
19
23
6746
59049
387420489
430467221
373248
Output:

Code: Select all

0
1
2
7
9
25
68
268
29
45555555588
-1
-1
-1
-1
-1
-1
99999
999999999
-1
888999

Re: 993 - Product of digits

Posted: Wed Jan 28, 2015 4:46 am
by xnorax
got AC-ed :D

Re: 993 - Product of digits

Posted: Sun Sep 27, 2015 9:02 am
by dTanMan
Hiya, I keep getting Runtime Errors and I can't fathom why. Help, please? :)

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";
	}
}

Re: 993 - Product of digits

Posted: Sat Oct 10, 2015 5:22 am
by brianfry713
use class Main