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; }
相关文章推荐
- USACO Section 1.3 Calf Flac
- USACO Section 1.3: Calf Flac
- USACO Section 1.3 Wormholes 解题报告
- USACO Section 1.3 wormhole (暴力+模拟)
- USACO Section 1.3 Combination Lock 解题报告
- USACO Section 1.3 : Barn Repair
- USACO Section 1.3 Mixing Milk
- USACO Section 1.3 Barn Repair
- USACO Section 1.3 : Calf Flac
- USACO Section 1.3 Barn Repair
- USACO Section1.3 混合牛奶
- USACO Section 1.3
- USACO Section 1.3 Calf Flac
- USACO Section1.3 Mixing Milk 解题报告
- USACO section 1.3
- USACO Section 1.3 Mixing Milk
- USACO Section 1.3 Prime Cryptarithm 解题报告
- USACO Section 1.3 Barn Repair
- USACO Section 1.3 Greedy Algorithm
- USACO section 1.3 Mixing Milk[贪心算法]