这道题还是可以说是比较简单的,一个三位数一个两位数,总共五个数字,保存起来就好了,循环遍历,再剪枝就可以了。还需要判断一下中间过程的数字以及结果。代码如下。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int bas[15];
int search3(int x){
if (100<=x&&x<=999){
for (int i=1;i<=3;i++){
int y=0;
y=x%10;
x/=10;
if(bas[y]==0){
return 0;
}
}
return 1;
}
return 0;
}
int search4(int x){
if (1000<=x&&x<=9999){
for (int i=1;i<=4;i++){
int y=0;
y=x%10;
x/=10;
if(bas[y]==0){
return 0;
}
}
return 1;
}
return 0;
}
int main (){
freopen ("crypt1.in","r",stdin);
freopen ("crypt1.out","w",stdout);
int n=0;
cin>>n;
int a=0,b=0,c=0,d=0,e=0,cnt=0;
int s1=0,s2=0,s3=0,s4=0,s5=0;
int one=0,two=0,thr=0,fou=0,fiv=0,count=0;
int key[15];
memset(bas,0,sizeof(bas));
memset(key,0,sizeof(key));
for (a=1;a<=n;a++){
cin>>b;
bas[b]++;
b=0;
}
for (a=1;a<=9;a++){
if (bas[a]>0){
key[cnt++]=a;
}
}
for (a=0;a<cnt;a++){
s1=key[a];
for (b=0;b<cnt;b++){
s2=key[b];
for (c=0;c<cnt;c++){
s3=key[c];
for (d=0;d<cnt;d++){
s4=key[d];
if (s1*s4>10){
break;
}
for (e=0;e<cnt;e++){
s5=key[e];
if (s1*s5>10){
break;
}
one=s1*100+s2*10+s3;
two=s4*10+s5;
thr=one*s5;
fou=one*s4;
fiv=one*two;
if (search3(thr)&&search3(fou)&&search4(fiv)){
count++;
}
}
}
}
}
}
cout<<count<<endl;
return 0;
}