USACO Section1.3 Prime Cryptarithm 解题报告

    crypt1解题报告 —— icedream61 博客园(转载请注明出处)
------------------------------------------------------------------------------------------------------------------------------------------------
【题目】
  用给出的N个数字,替换以下竖式,能生成多少个正确的竖式?
  * * *
  x * *
  -------
  * * *
  * * *
  -------
  * * * *
  输入文件,第一行N表示有N个数字,第二行给出所有数字(空格分割。)
【数据范围】
  所有数字∈{1,2,3,4,5,6,7,8,9}
【输入样例】
  5
  2 3 4 6 8
【输出样例】
  1
------------------------------------------------------------------------------------------------------------------------------------------------
【分析】
  竖式中,只要知道乘数和被乘数,就可以计算出下面三个结果。
  因此,只要枚举这两个数就好,一共5位,9^5<100000,一定没问题。
------------------------------------------------------------------------------------------------------------------------------------------------
【总结】
  一遍AC。
  样例一开始本机测试没过,原因是乘出来的三个结果没判断位数是否正确。可以说,还是读题不够严谨仔细。

------------------------------------------------------------------------------------------------------------------------------------------------

【代码】

 1 /*
 2 ID: icedrea1
 3 PROB: crypt1
 4 LANG: C++
 5 */
 6 
 7 #include <iostream>
 8 #include <fstream>
 9 using namespace std;
10 
11 int N,D[10];
12 bool have[10];
13 
14 bool ok(int num)
15 {
16     while(num)
17     {
18         if(!have[num%10]) return false;
19         num/=10;
20     }
21     return true;
22 }
23 bool ok(int A,int B,int C,int D,int E)
24 {
25     int i = A*100+B*10+C;
26     int j = D*10+E;
27     int x = i*E; if(x<100 || x>999) return false;
28     int y = i*D; if(y<100 || y>999) return false;
29     int z = x+y*10; if(z<1000 || z>9999) return false;
30     return ok(x) && ok(y) && ok(z);
31 }
32 
33 int main()
34 {
35     ifstream in("crypt1.in");
36     ofstream out("crypt1.out");
37 
38     in>>N;
39     for(int i=1;i<=N;++i) { in>>D[i]; have[D[i]]=true; }
40 
41     int s=0;
42     for(int a=1;a<=N;++a)
43         for(int b=1;b<=N;++b)
44             for(int c=1;c<=N;++c)
45                 for(int d=1;d<=N;++d)
46                     for(int e=1;e<=N;++e)
47                         if(ok(D[a],D[b],D[c],D[d],D[e])) ++s;
48     out<<s<<endl;
49 
50     in.close();
51     out.close();
52     return 0;
53 }

 

posted on 2015-03-09 12:35  IceDream61  阅读(205)  评论(0编辑  收藏  举报

导航