USACO-Section1.3 Prime Cryptarithm
2017-05-31 16:19
417 查看
2017-05-31
题目大意:
下面是一个乘法竖式,如果用我们给定的那n个数字来替代*,可以使式子成立的话,我们就叫这个式子牛式。 *** x ** ---------- *** *** ---------- **** 数字只能取代*,当然第一位不能为0,况且给定的数字里不包括0。 注意一下在美国的学校中教的“部分乘积”,第一部分乘积是第二个数的个位和第一个数的积,第二部分乘积是第二个数的十位和第一个数的乘积. 写一个程序求出所有牛式的总数。
样例输入:
5 2 3 4 6 8
样例输出:
1
题解:
题目采用深度优先搜索,在搜索的终点进行判断。
代码:
#include<iostream> #include<fstream> #include<algorithm> #include<cstring> using namespace std; int n; int num[10]; int vis[10]; int numPermu[10]; int cnt = 0; int weishu[3] = {3,3,4}; bool is_true(int _num){ for(int i = 0;i < n;i++){ if(_num == num[i]) return true; } return false; } int numToArray(int i , int _num){ int tmp = 0; int a[10]; while(_num){ a[tmp++] = _num % 10; _num /= 10; if(!is_true(a[tmp - 1])){ return false; } } if(tmp != weishu[i] ) return false; return true; } void dfs(int step){ if(step == 5){ int num1 = 100 *numPermu[0] + 10 * numPermu[1] + numPermu[2]; int num2 = 10 * numPermu[3] + numPermu[4]; int num3 = num1 * numPermu[3]; int num4 = num1 * numPermu[4]; int num5 = num1 * num2; if(numToArray(0 , num3) && numToArray(1 , num4) && numToArray(2 , num5)){ cnt++; return ; } return ; } for(int i = 0;i < n;i++){ numPermu[step] = num[i]; dfs(step + 1); numPermu[step] = 0; } } int main(){ ofstream cout("crypt1.out"); ifstream cin ("crypt1.in"); cin >> n; if(n == 0){ cout << 0 << endl; return 0; } for(int i = 0;i < n;i++){ cin >> num[i]; } memset(numPermu , 0 , sizeof(numPermu)); dfs(0); cout << cnt << endl; return 0; }
相关文章推荐
- [USACO]Section 1.3 Prime Cryptarithm
- USACO Section1.3 Prime Cryptarithm 解题报告
- USACO-Section 1.3 Prime Cryptarithm[...]
- USACO Section 1.3 Prime Cryptarithm
- USACO Section 1.3 Prime Cryptarithm
- USACO-Section 1.3 Prime Cryptarithm(枚举)
- USACO - Chapter1 Section 1.3 - Prime Cryptarithm
- USACO Section 1.3: Prime Cryptarithm
- USACO: Section 1.3 -- PROB Prime Cryptarithm
- USACO Section 1.3 : Prime Cryptarithm
- USACO-Section1.3 Prime Cryptarithm
- [USACO1.3]牛式 Prime Cryptarithm
- USCAO section1.3 Prime Cryptarithm(感觉思路挺好)
- USACO 1.3 Prime Cryptarithm
- USCAO section1.3 Prime Cryptarithm(感觉思路挺好)
- USACO section 1.3.4 Prime Cryptarithm
- USACO 1.3 Prime Cryptarithm
- usaco 1.3 Prime Cryptarithm
- USACO 1.3 Prime Cryptarithm
- P1211 [USACO1.3]牛式 Prime Cryptarithm