• USACO 1.3-Prime Cryptarithm


    /*
    ID: m1590291
    TASK: crypt1
    LANG: C++
    */
    #include <iostream>
    #include <fstream>
    #include <string.h>
    using namespace std;
    /******************************************************************************************************************
                    穷举法进行剪枝,
                    设置一个标志数组 a[],初值为0,当给定数字时赋值为1
                    对所有数进行判断进行剪枝即可
    ******************************************************************************************************************/
    int a[15];
    bool fuc(int x)
    {
        while(x!=0)
        {
            if(a[(x-(x/10)*10)] == 0)
                return false;
            x/=10;
        }
        return true;
    }
    int main()
    {
        ifstream fin("crypt1.in");
        ofstream fout("crypt1.out");
    
        int T,temp;
        while(fin>>T)
        {
            int num=0;
            memset(a,0,sizeof(a));
    
            for(int i = 1;i <= T;i ++){
                fin>>temp;
                a[temp]=1;
            }
            for(int i = 111;i <= 999;i ++){
                for(int j = 11;j <= 99;j ++){
                    if(fuc(i) && fuc(j)){    //剪枝1
                        int a=i*(j-(j/10)*10);
                        int b=i*(j/10);
                        if(a >= 111 && a<= 999 && b >= 111 && b <= 999 && fuc(a) && fuc(b)){    //剪枝2
                            int c=b*10+a;
                            if(fuc(c))    //符合所有条件,计数器 num+1
                                num++;
                        }
                    }
                }
            }
            fout<<num<<endl;
        }
        fin.close();
        fout.close();
        return 0;
    }
    


  • 相关阅读:
    【javaSE】Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
    property
    多继承与super
    GIL全局解释器锁
    深浅拷贝
    生成器
    迭代器
    设置ll命令
    修改Centos中的ll命令(以 K 为单位显示文件大小)
    打包解压缩命令
  • 原文地址:https://www.cnblogs.com/Jstyle-continue/p/6352035.html
Copyright © 2020-2023  润新知