USACO Prime Cryptarithm

本文介绍了一种名为Prime Cryptarithm的数学问题,通过使用指定集合中的数字填充空缺位置来解决乘法谜题。文章提供了一个C++程序示例,用于找到所有可能的解,并详细解释了实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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/archive/2013/04/01/5134200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值