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.
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 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.
题目给n个数字,要求找出所有符合要求的 3位数(abc)乘以2位数(de) 的组合
①abc和de由规定数字组成
②abc*d与abc*e的结果是三位数且由规定数字组成
③abc*de的结果是四位数且由规定数字组成
由于最多只有9个数字,所以枚举每个数字即可
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; }
相关文章推荐
- Oracle Study之---Oracle SQL语句中with的使用
- R语言_功效分析
- 使用setBounds()方法需要注意的地方
- 最快排序之"桶排序"
- 二维数组指针
- linux总结应用之六 驱动设备块设备中的中断
- HTML中rel属性分析
- jetty中禁止使用chunked传输数据
- ZooKeeper伪分布式安装
- Java之m2eclipse插件安装
- 自动轮播图
- 排序
- mfc 窗口控件全屏显示
- 我的人生1
- PHP 正则表达式
- Java多线程 -- 线程的交互
- Netty之有效规避内存泄漏
- php对接移动支付宝流程以及商户的私钥、公钥说明
- LeetCode 177 Nth-Highest Salary mysql,取第n条数据,limit子句 难度:1
- 详细的导入导出实例 (20150806)