2017-5-27
题目描述
给你几个数字,求出给定等式中数字都在这几个数中的数的个数
解答
深搜,深度只要到5即可
代码
/*
ID: 18795871
PROG: crypt1
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
ifstream fin("crypt1.in");
ofstream fout("crypt1.out");
bool f[10];
int x[10],j,sum=0,p,q,r;
int cal1(int n){
int r=0;
while (n){
r++;
n/=10;
}
return r;
}
bool cal2(int n){
while (n){
if (!f[n%10]) return false;
n/=10;
}
return true;
}
bool res(){
p=(100*x[1]+10*x[2]+x[3])*x[5];
q=(100*x[1]+10*x[2]+x[3])*x[4];
r=10*q+p;
if (cal1(p)==3&&cal2(p)&&cal1(q)==3&&cal2(q)&&cal1(r)==4&&cal2(r)) return true;
return false;
}
void dfs(int step){
if (step==6){
if (res()){
sum++;
}
return ;
}
for (int i=1;i<=9;i++){
if (f[i]){
x[step]=i;
dfs(step+1);
}
}
}
int main()
{
int n,m;
j=0;
fin>>n;
memset(f,false,sizeof(f));
memset(x,0,sizeof(x));
for (int i=1;i<=n;i++){
fin>>m;
f[m]=true;
}
dfs(1);
fout<<sum<<endl;
return 0;
}
简单的搜索题吧!只要把所有的情况找出来然后再判断是否符合题意即可。
/*
ID: 18795871
PROG: crypt1
LANG: C++
*/
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
ifstream fin("crypt1.in");
ofstream fout("crypt1.out");
const int N = 10;
bool f[N+1];
int x[5],cnt;
bool jud(int n,int l){
int num=0;
while (n){
if (!f[n%10]) return false;
n/=10;
num++;
}
if (num>l) return false;
return true;
}
bool isOK(){
int p=(100*x[0]+10*x[1]+x[2])*x[4];
int q=(100*x[0]+10*x[1]+x[2])*x[3];
if (jud(p,3)&&jud(q,3)&&jud(p+q*10,4)) return true;
return false;
}
void dfs(int step){
if (step==5){
if (isOK()){
cnt++;
}
return ;
}
for (int i=0;i<N;i++){
if (f[i]){
x[step]=i;
dfs(step+1);
}
}
}
int main(){
int n,t;
while (fin>>n){
cnt=0;
memset(f,false,sizeof(f));
for (int i=0;i<n;i++){
fin>>t;
f[t]=true;
}
dfs(0);
fout<<cnt<<endl;
}
return 0;
}