The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRYPTARITHM.
* * * x * * ------- * * * <-- partial product 1 * * * <-- partial product 2 ------- * * * *Digits can appear only in places marked by `*'. Of course, leading zeroes are not allowed.
Note that the 'partial products' are as taught in USA schools. The first partial product is the product of the final digit of the second number and the top number. The second partial product is the product of the first digit of the second number and the top number.
Write a program that will find all solutions to the cryptarithm above for any subset of digits from the set {1,2,3,4,5,6,7,8,9}.
PROGRAM NAME: crypt1
INPUT FORMAT
Line 1: | N, the number of digits that will be used |
Line 2: | N space separated digits with which to solve the cryptarithm |
SAMPLE INPUT (file crypt1.in)
5 2 3 4 6 8
OUTPUT FORMAT
A single line with the total number of unique solutions. Here is the single solution for the sample input:
2 2 2 x 2 2 ------ 4 4 4 4 4 4 --------- 4 8 8 4
SAMPLE OUTPUT (file crypt1.out)
1
本题还是比较容易理解的,只是乍一看题目不知道从何入手。本章节提示说使用Greed,但是并不是很好看出怎么“贪婪”入手。题目和编程之美中《4.10数字哑谜和回文》看起来有些相似,但是却不尽然。
为了简化本题,首先是试探$$$*$=$$$的可取值,然后再做进一步处理。
import java.io.BufferedReader; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; public class crypt1 { private static int[] res_digits = new int[10]; /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { // TODO Auto-generated method stub BufferedReader br = new BufferedReader(new FileReader("crypt1.in")); FileWriter fout = new FileWriter("crypt1.out"); int dig_number = Integer.parseInt(br.readLine()); ArrayList<Integer> list = new ArrayList<Integer>(); int tmp = 0 ; String[] source = br.readLine().split(" "); for(int i = 0;i<dig_number;i++){ tmp = Integer.parseInt(source[i]); list.add(tmp); res_digits[tmp] = 1; } Collections.sort(list); tmp = 0 ; int counter = 0; int result = 0; for(int h = 0;h<dig_number;h++){ for(int t = 0;t<dig_number;t++){ for(int s = 0;s<dig_number;s++){ HashSet<Integer> set = new HashSet<Integer>(); for(int n = 0;n<dig_number&&(list.get(h)*list.get(n)<10);n++){ tmp = list.get(h)*100+list.get(t)*10+list.get(s); tmp = tmp * list.get(n); if(check(tmp,3)){ set.add(tmp); Iterator<Integer> iterator = set.iterator(); while(iterator.hasNext()){ result = (Integer)iterator.next(); //this product is the result of head partial of second plus fisrt if(check(tmp*10 +result,4)){ counter++; System.out.println(list.get(h)+" "+list.get(t)+" "+list.get(s)+" "+(tmp*10 +result)); } //this product is the result of tail partial of second plus fisrt if((tmp*10 +result!=tmp +result*10)&&check(tmp +result*10,4)){ counter++; System.out.println(list.get(h)+" "+list.get(t)+" "+list.get(s)+" "+(tmp +result*10)); } } } } } } } fout.write(counter+"\n"); fout.flush(); fout.close(); br.close(); System.exit(0); } private static boolean check(int tmp, int i) { int ten_i = (int)Math.pow(10, i); int ten_i_1 = (int)Math.pow(10, i-1); if(tmp/ten_i>0||tmp/ten_i_1<0) return false; while(i>0){ if(res_digits[tmp%10]!=1){ return false; } tmp = tmp/10; i--; } return true; } }