zoukankan      html  css  js  c++  java
  • 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;
        }
    }

  • 相关阅读:
    数据库自定义表值函数Split(@LongStr, @SplitStr, @IsDistinct )
    牛客_{}大括号里面的内容都会执行,如果它不是成员函数的时候,看成是构造函数中的方法;
    剑指offer——替换字符串
    剑指offer_快速查找递增二维数组中是否存在目标
    IP地址理解_IP地址=网络地址+主机地址,但是具体前面多少是网络地址看题目说明
    TCP/IP三次握手
    牛客_剑指offer_重建二叉树,再后续遍历_递归思想_分两端
    牛客OJ——[编程题]A+B和C__如何输入多组测试数据(测试OK)
    学术_聚类种类分析(1)(转载)
    HW-找7(测试ok满分注意小于等于30000的条件)
  • 原文地址:https://www.cnblogs.com/amourjun/p/5134200.html
Copyright © 2011-2022 走看看