您的位置:首页 > 其它

USACO section 1.3 Prime Cryptarithm

2017-06-05 15:05 309 查看
这个程序主要分三步:

第一步:全排列,由于只需要五个数,所以排到第五个数就停止,没有进行什么剪枝操作,因为时间完全是允许的

第二步:计算并保存需要检测的值,手动写公式。。。没什么技术含量

第三步:将需要的结果挨个检测一下,符合万事大吉,不符合就江湖再见。

/*
ID: 13913351
LANG: C++
TASK: crypt1
*/
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
const int N=11;
int n;
int a
;
int res[5];
bool flag
;
int sum=0;
bool valid(int x,int m)   //判断过程中的各个变量是否符合“牛式”
{
while(m--)
{
if(flag[x%10]==false)
{
return false;
}
x/=10;
}
if(x!=0)return false;
return true;
}
bool test(int res[5])  //模拟乘积得到结果
{
int x=res[2]*res[4]+res[1]*res[4]*10+res[0]*res[4]*100;
int y=res[2]*res[3]+res[1]*res[3]*10+res[0]*res[3]*100;
int z=y*10+x;
if(valid(x,3)&&valid(y,3)&&valid(z,4))
{
return true;
}
return false;
}
void dfs(int step)
{
if(step==5)
{
if(test(res))
{
sum++;
}
return ;
}
for(int i=0;i<n;i++)
{
res[step]=a[i];
dfs(step+1);
}
}
int main()
{
memset(flag,false,sizeof(flag));
ifstream cin;
ofstream cout;
cin.open("crypt1.in");
cout.open("crypt1.out");
cin>>n;
int i;
for(i=0;i<n;i++)
{
cin>>a[i];
flag[a[i]]=true;//将给出的数字进行标记
}
dfs(0);
cout<<sum<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: