993 - Product of digits

All about problems in Volume 9. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

mgavin2
New poster
Posts: 43
Joined: Sat Jul 28, 2012 6:29 pm

Re: 993 - Product of digits

Post by mgavin2 »

Thanks CSGrandeur... you made me realize I was factorizing the digits in the wrong direction :), instead of 2 -> 9
all that matters is AC
alimbubt
New poster
Posts: 39
Joined: Tue Aug 07, 2012 10:40 pm
Location: BUBT,Dhaka, Bangladesh
Contact:

Re: 993 - Product of digits

Post 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
Give me six hours to chop down a tree and I will spend the first four sharpening the axe...(BUBT ILLUSION)
http://uhunt.felix-halim.net/id/155497
http://onlyprogramming.wordpress.com/
xnorax
New poster
Posts: 9
Joined: Thu Jun 26, 2014 9:31 pm

Re: 993 - Product of digits

Post by xnorax »

got AC-ed :D
dTanMan
New poster
Posts: 5
Joined: Sat Feb 01, 2014 5:22 am

Re: 993 - Product of digits

Post 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";
	}
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 993 - Product of digits

Post by brianfry713 »

use class Main
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 9 (900-999)”