Prime Cryptarithm

来源:互联网 发布:佛经大全软件 编辑:程序博客网 时间:2024/06/15 01:39

题目:

Prime Cryptarithm

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

题意:

这题就是说用给出的数字可以把下图中的*替换掉,由于太过粗心这题做的时候犯了两次错误,一是自认为是只要把最终结果替换掉就可以,后来才看到还有中间两次结果的也得替换,第二次是只注意到了最终结果要求为四位数字,忽略了中间两次结果都为三位数字。这些都是审题时的失误,以后一定要用心,用心,用心。。。。

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

思路:

就是把做因数的五个数字一个一个的变,从a[0]~a[n-1],就可以了。

代码:

/*ID:fyhwyx1PROG:crypt1LANG:C*/#include<stdio.h>#include<string.h>int check(int g,int h,int a1[12]){    int r,flag,u;  while (g){    u=g%10;    for (r=0;r<h;r++)    {        if (u==a1[r])            break;    }    if (r>=h)    {      flag=0;      break;    }    else        flag=1;    g/=10;}if (flag)    return 1;    else        return 0;}//判断乘得的数是否每一位都是给出的数int main(){    freopen("crypt1.in","r",stdin);    freopen("crypt1.out","w",stdout);    int n,i,a[12],k,j,t,m,e,f,q,s,d=0;    scanf("%d",&n);    for (i=0;i<n;i++)        scanf("%d",&a[i]);    for (i=0;i<n;i++)    {        for (k=0;k<n;k++)        {            for (j=0;j<n;j++)            {                for (t=0;t<n;t++)                {                    for (m=0;m<n;m++)                    {                        e=a[i]*100+a[k]*10+a[j];//变换每一位的数,得到重新组合的算术式                        q=e*a[m];                        if (q>99&&q<=999)                        s=check(q,n,a);//判断个位与三位数相乘的结果                        else                            s=0;                        if (!s)                            continue;                        else                        {                            q=e*a[t];                        if (q>99&&q<=999)                            s=check(q,n,a);//判断十位与三位数相乘的结果                            else                                s=0;                            if (!s)                                break;                            else                            {                                q=e*(a[t]*10+a[m]);                                if (q>999&&q<=9999)                                s=check(q,n,a);//判断这个两位数与三位数相乘的结果                                else                                    s=0;                                if (s)                                    d++;                                else                                    continue;                            }                        }                    }                }            }        }    }    printf("%d\n",d);    return 0;}


0 0
原创粉丝点击