[USACO]Prime Cryptarithm【JAVA】

来源:互联网 发布:广电网络宽带怎么缴费 编辑:程序博客网 时间:2024/05/20 09:27
PrimeCryptarithm

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 usedLine 2:N space separated digits with which to solve thecryptarithm

SAMPLE INPUT (file crypt1.in)

52 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 (filecrypt1.out)

1
暴力破解,但在范围选择,树上上加以判定缩短运行时间
import java.io.*;
import java.util.*;

public class crypt1 {
      private static int array[];

      public static void main(String[] args) throws IOException {
              // Use BufferedReader rather than RandomAccessFile; it's much faster
              BufferedReader f = new BufferedReader(new FileReader("crypt1.in"));
              // input file name goes above
              PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
                              "crypt1.out")));
              int n = Integer.parseInt(f.readLine());
              StringTokenizer st = new StringTokenizer(f.readLine());
              int count = 0;
          
              array = new int[n];
              for(int i = 0; i< n;i++){
                      array[i] = Integer.parseInt(st.nextToken());
              }
          
              for(int i = 111; i<=9999/11;i++){
                      if(check(i)){
                              for(int j = 11;j <= ((999/i) * 11); j++){
                                      if(handle(i,j)&&(j * i <=999)){
                                              count++;
                                      }
                              }
                      }
              }
              out.println(count);
              out.close(); // close the output file
              System.exit(0); // don't omit this!
      }
  
      private static boolean handle(int num1, int num2){
              boolean result = true;
              if(check(num1) && check(num2) && check(num1 * num2)){
                      while(num2 != 0){
                              if(check(num1*(num2))){
                                      num2 = num2/10;
                              }else{
                                      result = false;
                                      break;
                              }
                      }
              }else{
                      result = false;
              }
              return result;
      }
  

      private static boolean check(int num){
              boolean result = true;
              while(num !=0){
                      if(!isset(num)){
                              result = false;
                              break;
                      }else{
                              num = num /10;
                      }
              }
              return result;
      }
  
      private static boolean isset(int num){
              boolean result = false;
              for(int i = 0; i
                      if(array[i] == num){
                              result = true;
                              break;
                      }
              }
              return result;
      }
}



USER: Singles Xin [xl1993a1]TASK: crypt1LANG: JAVACompiling...Compile: OKExecuting...   Test 1: TEST OK [0.130 secs, 30540 KB]   Test 2: TEST OK [0.108 secs, 30540 KB]   Test 3: TEST OK [0.108 secs, 30540 KB]   Test 4: TEST OK [0.130 secs, 30540 KB]   Test 5: TEST OK [0.144 secs, 30540 KB]   Test 6: TEST OK [0.137 secs, 30540 KB]   Test 7: TEST OK [0.144 secs, 30540 KB]All tests OK.
Your program ('crypt1') produced all correct answers! This is yoursubmission #8 for this problem. Congratulations!

Here are the test data inputs:

------- test 1 ----52 3 4 6 8------- test 2 ----42 3 5 7------- test 3 ----11------- test 4 ----74 1 2 5 6 7 3------- test 5 ----89 1 7 3 5 4 6 8------- test 6 ----61 2 3 5 7 9------- test 7 ----91 2 3 4 5 6 7 8 9
Keep up the goodwork!
Thanksfor your submission!
0 0