您的位置:首页 > 其它

USACO-Section 1.3 Prime Cryptarithm(枚举)

2015-08-26 15:43 381 查看
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


题目给n个数字,要求找出所有符合要求的 3位数(abc)乘以2位数(de) 的组合
①abc和de由规定数字组成

②abc*d与abc*e的结果是三位数且由规定数字组成

③abc*de的结果是四位数且由规定数字组成

由于最多只有9个数字,所以枚举每个数字即可

/*
ID: your_id_here 
PROG: crypt1
LANG: C++
*/
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
int ans,n,num[10],a,b,c,t1,t2,fir;
bool used[10];

inline bool judge(int x) {//判断数x是否只含有规定数字
while(x) {
if(!used[x%10])
return false;
x/=10;
}
return true;
}

int main() {
int i,j,k,l,m;
freopen("crypt1.in","r",stdin);
freopen("crypt1.out","w",stdout);
while(1==scanf("%d",&n)) {
memset(used,false,sizeof(used));
for(i=0;i<n;++i) {
scanf("%d",num+i);
used[num[i]]=true;
}
ans=0;
for(i=0;i<n;++i) {
if((a=num[i])!=0)
for(j=0;j<n;++j) {
b=num[j];
for(k=0;k<n;++k) {
fir=a*100+b*10+num[k];
for(l=0;l<n;++l) {
if((c=num[l])!=0&&(t1=fir*c)<1000&&judge(t1))
for(m=0;m<n;++m) {
if((t2=fir*num[m])<1000&&judge(t2)&&judge(t1*10+t2))
++ans;
}
}
}
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: