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

解题报告:

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

细节看注释吧……

AC代码:

/*
ID: yuanmz91
PROG: crypt1
LANG: 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 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
中文版

描述

下面是一个乘法竖式,如果用我们给定的那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

5
2 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 ----
5
2 3 4 6 8
------- test 2 ----
4
2 3 5 7
------- test 3 ----
1
1
------- test 4 ----
7
4 1 2 5 6 7 3
------- test 5 ----
8
9 1 7 3 5 4 6 8
------- test 6 ----
6
1 2 3 5 7 9
------- test 7 ----
9
1 2 3 4 5 6 7 8 9

Keep up the good work!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值