您的位置:首页 > 编程语言 > C语言/C++

USACO Section 1.3 Prime Cryptarithm

2014-10-15 16:34 1156 查看

题目原文

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.
The partial products must be three digits long, even though the general case (see below) might have four digit partial products. 

********** Note About Cryptarithm's Multiplication ************ 

In USA, children are taught to perform multidigit multiplication as described here. Consider multiplying a three digit number whose digits are 'a', 'b', and 'c' by a two digit number whose digits are 'd' and 'e':
[Note that this diagram shows far more digits in its results than
the required diagram above which has three digit partial products!]

a b c     <-- number 'abc'
x   d e     <-- number 'de'; the 'x' means 'multiply'
-----------
p1      * * * *     <-- product of e * abc; first star might be 0 (absent)
p2    * * * *       <-- product of d * abc; first star might be 0 (absent)
-----------
* * * * *     <-- sum of p1 and p2 (e*abc + 10*d*abc) == de*abc

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 supplied non-zero single-digits.

PROGRAM NAME: crypt1

INPUT FORMAT

Line 1:N, the number of digits that will be used
Line 2:N space separated non-zero 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 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


分析

题目的意思非常简单,给定几个数字,然后看看有几个三位数乘两位数的竖式乘法整个过程的每一个数字都包含在给定的数字中,比如例子中,输入
2 3 4 6 8

其中的

2 2 2
x   2 2
------
4 4 4
4 4 4
---------
4 8 8 4
就是满足的一个例子,要求求出所有满足的竖式个数。

这个题目的解法简单粗暴,直接穷举就可以了,穷举的次数为(999-99)*(99-9)次。

代码实现

首先输入数据

<span style="white-space:pre">	</span>ifstream fin("crypt1.in");
ofstream fout ("crypt1.out");

int n;
fin >> n;
vector<bool> digits(10);


这里用一个长度为10的布尔数组来表示数字0~9有哪些是已经输入的

然后做一个函数来判断任意一个整数每一位的数字是否都在输入的几个数字中

bool isInSets(int a, const vector<bool> &digits)
{
bool out = true;

while(a / 10 != 0)
{
out = out && digits[a%10];
a = (a - a % 10) / 10;
if(!out)
return false;
}
out = out && digits[a];

return out;
}


然后穷举就可以了。
完整的代码如下:

/*
ID:
PROG: crypt1
LANG: C++
*/

#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <map>
#include <iostream>
using namespace std;

bool isInSets(int a, const vector<bool> &digits)
{
bool out = true;

while(a / 10 != 0)
{
out = out && digits[a%10];
a = (a - a % 10) / 10;
if(!out)
return false;
}
out = out && digits[a];

return out;
}

int main()
{
ifstream fin("crypt1.in");
ofstream fout ("crypt1.out");

int n;
fin >> n;
vector<int> numbers(n);
vector<bool> digits(10);
for (int i=0;i!=n;i++)
{
fin >> numbers[i];
digits[numbers[i]] = true;
}

int count = 0;
for (int i=100;i!=1000;i++)
{
for (int j=10;j!=100;j++)
{
if(i * j> 9999 || i * (j % 10) > 999 || i * (j / 10) > 999)
continue;

if(isInSets(i,digits) && isInSets(j,digits) && isInSets(i * (j % 10),digits) && isInSets(i * (j / 10),digits) && isInSets(i*j,digits))
{
count++;
}
}
}

fout << count << endl;

return 0;

}


提交结果

TASK: crypt1
LANG: C++

Compiling...
Compile: OK

Executing...
Test 1: TEST OK [0.003 secs, 3500 KB]
Test 2: TEST OK [0.005 secs, 3500 KB]
Test 3: TEST OK [0.003 secs, 3500 KB]
Test 4: TEST OK [0.003 secs, 3500 KB]
Test 5: TEST OK [0.003 secs, 3500 KB]
Test 6: TEST OK [0.003 secs, 3500 KB]
Test 7: TEST OK [0.003 secs, 3500 KB]


官方给出的参考答案

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int isgooddigit[10];	/* isgooddigit[d] is set if d is an acceptable digit
*/

/* check that every decimal digit in "n" is a good digit,
and that it has the right number "d" of digits. */
int
isgood(int n, int d)
{
if(n == 0)
return 0;

while(n) {
if(!isgooddigit[n%10])
return 0;
n /= 10;
d--;
}

if( d == 0 )
return 1;
else
return 0;
}

/* check that every product line in n * m is an okay number */
int
isgoodprod(int n, int m)
{
if(!isgood(n,3) || !isgood(m,2) || !isgood(n*m,4))
return 0;

while(m) {
if(!isgood(n*(m%10),3))
return 0;
m /= 10;
}
return 1;
}

void
main(void)
{
int i, j, n, nfound;
FILE *fin, *fout;

fin = fopen("crypt1.in", "r");
fout = fopen("crypt1.out", "w");
assert(fin != NULL && fout != NULL);

for(i=0; i<10; i++) {
isgooddigit[i] = 0;
}
fscanf(fin, "%d", &n);
for(i=0; i<n; i++) {
fscanf(fin, "%d", &j);
isgooddigit[j] = 1;
}

nfound = 0;
for(i=100; i<1000; i++)
for(j=10; j<100; j++)
if(isgoodprod(i, j))
nfound++;

fprintf(fout, "%d\n", nfound);
exit(0);
}
没啥好说的,官方的参考答案也基本类似

THE END

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ USACO