2312--1.3.4 Prime Cryptarithm 牛式

Description

下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。

      * * *
   x    * *
    -------
      * * *
    * * *
    -------
    * * * *

数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0。

注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积.

写一个程序找出所有的牛式。

Input

Line 1:数字的个数n。 Line 2:N个用空格分开的数字(每个数字都∈ {1,2,3,4,5,6,7,8,9})。

Output

 共一行,一个数字。表示牛式的总数。
 下面是样例的那个牛式。

        2 2 2
      x   2 2
    ---------
        4 4 4
      4 4 4
    ---------
     4 8 8 4

Sample Input

5
2 3 4 6 8

Sample Output

1

题意:给你n个数字,使其乘法竖式里的每一个数字都是给出的数字,求牛式的个数。
分析:a满足100——999,b满足10——99,两个循环遍历,再判断每个数字是否是给出的那些数字,不是就继续找。判断:把出现的数字先标记一下
 1 #include<cstdio>
 2 #include<cstring>
 3 int a[1005];
 4 int judge(int x)
 5 {
 6     while(x)
 7     {
 8         if(a[x%10]==0)
 9             return 1;
10         x/=10;
11     }
12     return 0;
13 }//判断这个数字里面的数字是否是给出的
14 int main()
15 {
16     int n,m;
17     while(~scanf("%d",&n))
18     {
19         memset(a,0,sizeof(a));
20         for(int i=1;i<=n;i++)
21         {
22             scanf("%d",&m);
23             a[m]=1;
24         }//标记
25         int ans=0;
26         for(int i=100;i<=999;i++)
27         {
28             if(judge(i)==1)
29                 continue;
30             for(int j=10;j<=99;j++)
31             {
32                 int ans1;
33                 ans1=i*j;
34                 if(ans1>9999||judge(j)==1||judge(ans1)==1)
35                 continue;
36                 int ans2;
37                 ans2=i*(j%10);
38                 if(ans2>999||judge(ans2)==1)
39                     continue;
40                 int ans3;
41                 ans3=i*(j/10);
42                 if(ans3>999||judge(ans3)==1)
43                     continue;
44                 ans++;
45             }
46         }
47         printf("%d
",ans);
48     }
49    return 0;
50 }
原文地址:https://www.cnblogs.com/LLLAIH/p/9715006.html