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个元素,每个元素多少属于[1,9],要求 * 里面的数字都是这个集合里的,问满足这个集合的种数。
分析:直接枚举所以情况9^5在1s内,在判断。
我用两种判断来做效果很大的区别呀!!!
心得:代码一是起初写的,可是WA,所以就找bug呀,就是找不到,所以就改写代码二了,果断AC了,
用代码二生成的结果用代码一调试,才发现b[3][0]=b[2][0]+b[1][1]; b[2][0]和b[1][1] 忘了模10了,
总之以后不要写这样的代码了,极容易出错!!!
代码一:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 /* 2 ID: dizzy_l1 3 LANG: C++ 4 TASK: crypt1 5 */ 6 #include<fstream> 7 #include<iostream> 8 using namespace std; 9 10 bool flag[10]; 11 int a[10],b[4][5],n,ans; 12 13 bool work() 14 { 15 b[1][0]=b[0][0]*b[0][2]; 16 if(!flag[b[1][0]%10]) return false; 17 b[1][1]=b[0][0]*b[0][3]+b[1][0]/10; 18 if(!flag[b[1][1]%10]) return false; 19 b[1][2]=b[0][0]*b[0][4]+b[1][1]/10; 20 if(b[1][2]>9||!flag[b[1][2]]) return false; 21 22 b[2][0]=b[0][1]*b[0][2]; 23 if(!flag[b[2][0]%10]) return false; 24 b[2][1]=b[0][1]*b[0][3]+b[2][0]/10; 25 if(!flag[b[2][1]%10]) return false; 26 b[2][2]=b[0][1]*b[0][4]+b[2][1]/10; 27 if(b[2][2]>9||!flag[b[2][2]%10]) return false; 28 29 b[3][0]=b[1][1]%10+b[2][0]%10; //忘了%10了 所以导致持续WA 30 if(!flag[b[3][0]%10]) return false; 31 b[3][1]=b[1][2]%10+b[2][1]%10+b[3][0]/10; 32 if(!flag[b[3][1]%10]) return false; 33 b[3][2]=b[2][2]%10+b[3][1]/10; 34 if(b[3][2]>9||!flag[b[3][2]%10]) return false; 35 return true; 36 } 37 38 void search(int i) 39 { 40 if(i==5) 41 { 42 if(work()) 43 ans++; 44 } 45 else if(i<5) 46 { 47 int k; 48 for(k=0;k<n;k++) 49 { 50 b[0][i]=a[k]; 51 search(i+1); 52 } 53 } 54 } 55 56 int main() 57 { 58 freopen("crypt1.in","r",stdin); 59 freopen("crypt1.out","w",stdout); 60 int i; 61 while(cin>>n) 62 { 63 ans=0; 64 for(i=0;i<10;i++) flag[i]=false; 65 for(i=0;i<n;i++) 66 { 67 cin>>a[i]; 68 flag[a[i]]=true; 69 } 70 search(0); 71 cout<<ans<<endl; 72 } 73 }
代码二:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
1 /* 2 ID: dizzy_l1 3 LANG: C++ 4 TASK: crypt1 5 */ 6 #include<fstream> 7 #include<iostream> 8 using namespace std; 9 10 bool flag[10]; 11 int a[10],b[4][5],n,ans; 12 13 bool judge_yes(int t) 14 { 15 int tt; 16 while(t) 17 { 18 tt=t%10; 19 if(!flag[tt]) return false; 20 t=t/10; 21 } 22 return true; 23 } 24 25 bool work() 26 { 27 int t1,t2,t3; 28 t3=b[0][4]*100+b[0][3]*10+b[0][2]; 29 30 t1=t3*b[0][0]; 31 if(t1>999) return false; 32 if(!judge_yes(t1)) return false; 33 34 t2=t3*b[0][1]; 35 if(t2>999) return false; 36 if(!judge_yes(t2)) return false; 37 38 t3=t1+t2*10; 39 if(t3>9999) return false; 40 if(!judge_yes(t3)) return false; 41 42 return true; 43 } 44 45 void search(int i) 46 { 47 if(i==5) 48 { 49 if(work()) 50 ans++; 51 } 52 else if(i<5) 53 { 54 int k; 55 for(k=0;k<n;k++) 56 { 57 b[0][i]=a[k]; 58 search(i+1); 59 } 60 } 61 } 62 63 int main() 64 { 65 freopen("crypt1.in","r",stdin); 66 freopen("crypt1.out","w",stdout); 67 int i; 68 while(cin>>n) 69 { 70 ans=0; 71 for(i=0;i<10;i++) flag[i]=false; 72 for(i=0;i<n;i++) 73 { 74 cin>>a[i]; 75 flag[a[i]]=true; 76 } 77 search(0); 78 cout<<ans<<endl; 79 } 80 }