The following cryptarithm is a multiplication problem that can besolved by substituting digits from a specified set of N digits into thepositions marked with *. If the set of prime digits {2,3,5,7} isselected, the cryptarithm is called a PRIME CRYPTARITHM.
* * * x * * ------- * * * <-- partial product 1 * * * <-- partial product 2 ------- * * * *Digits can appear only in places marked by `*'. Of course, leadingzeroes are not allowed.
The partial products must be three digits long, even though thegeneral case (see below) might have four digit partial products.
********** Note About Cryptarithm's Multiplication ************
In USA, children are taught to perform multidigit multiplicationas described here. Consider multiplying a three digit numberwhose digits are 'a', 'b', and 'c' by a two digit number whosedigits 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 thesecond number and the top number. The second partial product isthe product of the first digit of the second number and the topnumber.
Write a program that will find all solutions to the cryptarithmabove 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. Hereis 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 题意:所谓题目给出的牛式,就是要求用所给的数字来替代竖式中的*,然后找出所有满足条件的竖式的个数 思路:直接暴力被乘数和乘数,只要保证被乘数是3位数,乘数是2位数,得到的part1,part2是三位数,最后的结果是四位数,并且构成这些数的所有数字都必须是给出的数字 代码:PS:自己的英语水平很弱,题目读起来很费劲,但是读懂之后就很简单了,加油!!!/* ID: fanmeng PROG: crypt1 LANG: C++ */ #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <queue> #include <vector> #include <set> #include <map> using namespace std; int dig[10]; int isok[10]; bool judge(int x) { while(x) { if(!isok[x%10]) { return false; } x /= 10; } return true; } int main() { freopen("crypt1.in","r",stdin); freopen("crypt1.out","w",stdout); int n; cin >> n; memset(isok,false,sizeof(isok)); for(int i = 0; i < n; i++) { cin >> dig[i]; isok[dig[i]] = true; } int k = 0; for(int a = 0; a < n; a++) { for(int b = 0; b < n; b++) { for(int c = 0; c < n; c++) { for(int d = 0; d < n; d++) { for(int e = 0; e < n; e++) { int chengshu = dig[a]*100+dig[b]*10+dig[c]; int ss = dig[d]*10+dig[e]; int s1 = chengshu*ss; if(chengshu >= 100 && ss > 9 && s1 >= 1000 && s1 <= 9999) { int part1 = dig[d]*chengshu; int part2 = dig[e]*chengshu; if(part1 >= 100 && part1 <= 999 && part2 >= 100 && part2 <= 999) { if(judge(part1)&&judge(part2)&&judge(s1)) { k++; } } } } } } } } cout << k << endl; return 0; }