USACO Prime Cryptarithm

//这题是一简单的一个递归    算锻炼水体熟练度吧~~~

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  

/*
ID: jun41821
PROG: crypt1
LANG: C++
*/
#include <iostream>
#include <cstring>
#include <fstream>
#include <algorithm>
using namespace std;
ofstream fout ("crypt1.out");
ifstream fin ("crypt1.in");
int jude(int x,int n[10],int length);
int main()
{
    int mul1[1005],mul2[105],n[10];
    int m1,m2,n1,n2,n3,k=0,i,j,t,num1,num2,num,N;
    fin>>N;
    for(i=0;i<N;i++)
    {
        fin>>n[i];                      //输入可用的数
    }
    //可能出现的两位数
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            if(n[i]!=0)
            {
                mul2[k]=n[i]*10+n[j];
                k++;
            }
        }
    }
    num2=k+1;
    k=0;
    //肯能出现的三位数
    for(i=0;i<N;i++)
    {
        for(j=0;j<N;j++)
        {
            for(t=0;t<N;t++)
            {
                if(n[i]!=0)
                {
                    mul1[k]=n[i]*100+n[j]*10+n[t];
                    k++;
                }
            }
        }
    }
    num1=k+1;
    num=0;
    for(i=0;i<num1;i++)
        for(j=0;j<num2;j++)
        {
            if(mul2[j]%10*mul1[i]>=100&&mul2[j]%10*mul1[i]<=999&&mul2[j]/10*mul1[i]<=999&&mul2[j]/10*mul1[i]>=100)
                if(jude(mul2[j]%10*mul1[i],n,N)&&jude(mul2[j]/10*mul1[i],n,N))
                    if(mul2[j]*mul1[i]>=1000&&mul2[j]*mul1[i]<=9999)
                        if(jude(mul2[j]*mul1[i],n,N))
                            num++;
        }
    fout<<num<<endl;
    return 0;

}
int jude(int x,int n[],int length)
{
    int i=0,t=0,k=0,l=0,f=0;
    if(x>999&&x<=9999)      //四位数
    {
        for(i=0;i<length;i++)
        {
            if(x%10==n[i])      //个位
             t=1;
             if(x%100/10==n[i])
             k=1;
             if(x/100%10==n[i])
             l=1;
             if(x/1000==n[i])
             f=1;
        }
        if(t&&k&&l&&f)
        return 1;
        else return 0;
    }
    if(x>99&&x<=999)//三位数
    {
        for(i=0;i<length;i++)
        {
            if(x%10==n[i])      //个位
             t=1;
             if(x%100/10==n[i])
             k=1;
             if(x/100==n[i])
             l=1;
        }
        if(t&&k&&l)
        return 1;
        else return 0;
    }
    if(x>9&&x<=99)
    {
         for(i=0;i<length;i++)
        {
            if(x%10==n[i])      //个位
             t=1;
             if(x%100/10==n[i])
             k=1;
        }
        if(t&&k)
        return 1;
        else return 0;
    }
}

原文地址:https://www.cnblogs.com/amourjun/p/5134200.html