USACO Training Section 1.3 Prime Cryptarithm 解题报告&AC代码

来源:互联网 发布:知乐的小说哪本好看 编辑:程序博客网 时间:2024/06/05 07:35

解题报告:

这道题还是比较水的,遍历两个乘数,找出符合条件的即可。

细节看注释吧……

AC代码:

/*ID: yuanmz91PROG: crypt1LANG: C++*/#include <fstream>#include <memory.h>using namespace std;int digitnum;bool digit[10];bool Judge(int x){    while(x > 0)    {        if(digit[x % 10] == false)  //如果这个数字没有在字典里,那就不符合条件        {            return true;        }        x /= 10;    }    return false;  //如果一切安全,就符合条件}int main(){    ifstream fin("crypt1.in");    ofstream fout("crypt1.out");    int cnt = 0, buffer;    memset(digit, false, sizeof(digit));  //初始设置所有数字都不能出现    fin >> digitnum;    for(int i = 0; i < digitnum; ++i)    {        fin >> buffer;        digit[buffer] = true;  //如果数字a可以出现,那么字典中“a可用”即为真    }    for(int i = 111; i <= 999; ++i)    {        for(int j = 11; j <= 99; ++j)  //遍历i、j        {            if(Judge(i) || Judge(j))  //如果i、j本身不符合,直接跳过            {                continue;            }            int a, b, res;            a = i * (j % 10);            b = i * (j / 10);            res = i * j;            if((a > 999) || (b > 999) || (res > 9999))  //如果后面的结果位数不对,也跳过            {                continue;            }            if(Judge(a) || Judge(b) || Judge(res))  //如果后面的数字出现不符合,跳过            {                continue;            }            else            {                cnt++;  //最后,如果都符合条件,就可以认为多了一个了                //如果想输出算式,可以加上fout << i << " " << j << " " << a << " " << b << " " << res << endl;            }        }    }    fout << cnt << endl;  //输出答案    return 0;}

下面是题目:

英文版

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 usedLine 2:N space separated digits with which to solve the cryptarithm

SAMPLE INPUT (file crypt1.in)

52 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
中文版

描述

下面是一个乘法竖式,如果用我们给定的那n个数字来取代*,可以使式子成立的话,我们就叫这个式子牛式。

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

数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0。

注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积.

写一个程序找出所有的牛式。 

格式

PROGRAM NAME: crypt1

INPUT FORMAT:

(file crypt1.in)

 Line 1:数字的个数n。 Line 2:N个用空格分开的数字(每个数字都属于{1,2,3,4,5,6,7,8,9})。

OUTPUT FORMAT:

(file crypt1.out)

 共一行,一个数字。表示牛式的总数。

SAMPLE INPUT

52 3 4 6 8

SAMPLE OUTPUT

1

样例分析

        2 2 2    x     2 2   ----------        4 4 4      4 4 4   ----------      4 8 8 4

注意:结果只能为4位

测试数据:
Here are the test data inputs:------- test 1 ----52 3 4 6 8------- test 2 ----42 3 5 7------- test 3 ----11------- test 4 ----74 1 2 5 6 7 3------- test 5 ----89 1 7 3 5 4 6 8------- test 6 ----61 2 3 5 7 9------- test 7 ----91 2 3 4 5 6 7 8 9Keep up the good work!



原创粉丝点击