USACO-Section 1.3 Prime Cryptarithm[...]
来源:互联网 发布:sql 删除表结构 编辑:程序博客网 时间:2024/06/05 06:33
2015-05-30
心好痛。。每次总体思路都是正确的,而代码总有无数bug,,改代码改到生无可恋T_T……
所以,个人思维难免会有盲点,困在自己的世界里死循环,终究无解……所以不能狭隘地活着,,世界毕竟那么大。
题目大意:
给出N个非0的整数(就是1~9),把它们填入如下乘法式中的“*”处,使得算式成立。输出为不同填法的数目。
* * * x * * ------- * * * <-- partial product 1 * * * <-- partial product 2 ------- * * * *
题解:
将“*”位置用如下字符表示,则a,b,c,d,e的组合完全决定了其余值。
p1 = (a*e)+((b*e + c*e/10)/10);
p2 = (b*e+(c*e)/10)%10;
p3 = (c*e)%10;
p4 = (a*d)+((b*d + c*d/10)/10);
p5 = (b*d+(c*d)/10)%10;
p6 = (c*d)%10;
r1 = p4+((p1+p5)/10);
r2 = ((p1+p5)+(p2+p6)/10)%10;
r3 = (p2+p6)%10;
r4 = p3。
所以作五层循环,每层分别确定a,b,c,d,e。在d确定后,即可确定p4,p5,p6,而所有这些未知数只要有一个不属于给定的N个数那么此种填法不可行,无论e的取值如何。所以在确定d之后就判断p4,p5,p6,确定e之后再判断其他值。
a b c x d e ------------- p1 p2 p3 <-- partial product 1 p4 p5 p6 <-- partial product 2 ------------- r1 r2 r3 r4
我的bug就是没有仔细检查p1,p2,p3,p4,p5,p6和r1,r2,r3的表达式,最后看到别人的代码才恍然大悟T_T……总之,自己还是太菜了啊T_T
参考的代码链接如下:
链接
我的代码如下:
/*ID: madara01PROG: crypt1LANG: C++*/#include <iostream> #include <fstream>#include <string>#define cin fin#define cout fout#define MAX 10using namespace std;int n;int numbers[MAX];bool search(int num) { //判断num是否在numbers中 int i; for(i = 0; i < n; i++) { if(num == numbers[i]) return true; } return false;}int main(int argc, char **argv){ int result = 0; //结果 int i; //循环变量 int a,b,c,d,e; //乘数 int p1,p2,p3,p4,p5,p6; //中间结果 int r1,r2,r3; //最终结果前三位 ofstream fout ("crypt1.out"); ifstream fin ("crypt1.in"); cin >> n; for(i = 0; i < n; i++) cin >> numbers[i]; for(int i1 = 0; i1 < n; i1 ++) { //a a = numbers[i1]; for(int i2 = 0; i2 < n; i2 ++) { //b b = numbers[i2]; for(int i3 = 0; i3 < n; i3 ++) { //c c = numbers[i3]; for(int i4 = 0; i4 < n; i4 ++) { //d d = numbers[i4]; p4 = (a*d)+((b*d + c*d/10)/10); if(p4 == 0 || !search(p4)) continue; p5 = (b*d+(c*d)/10)%10; if(!search(p5)) continue; p6 = (c*d)%10; if(!search(p6)) continue; for(int i5 = 0; i5 < n; i5 ++) { //e e = numbers[i5]; p1 = (a*e)+((b*e + c*e/10)/10); if(p1 == 0 || !search(p1)) continue; p2 = (b*e+(c*e)/10)%10; if(!search(p2)) continue; p3 = (c*e)%10; if(!search(p3)) continue; r1 = p4+((p1+p5)/10); if(!search(r1)) continue; r2 = ((p1+p5)+(p2+p6)/10)%10; if(!search(r2)) continue; r3 = (p2+p6)%10; if(!search(r3)) continue; result ++; } } } } } cout << result << endl; return 0;}
阅读全文
0 0
- [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 section 1.3 Prime Cryptarithm
- USACO Section 1.3 Prime Cryptarithm
- USACO section 1.3.4 Prime Cryptarithm
- USACO Section 1.3.6 Prime Cryptarithm
- USACO Section 1.3.4 Prime Cryptarithm
- USACO-Section 1.3 Prime Cryptarithm(枚举)
- USACO-section 1.3 Prime Cryptarithm[hash]
- USACO Training Section 1.3 Prime Cryptarithm 解题报告&AC代码
- USACO 1.3-Prime Cryptarithm
- USACO 1.3 Prime Cryptarithm
- usaco 1.3 Prime Cryptarithm
- USACO 1.3 Prime Cryptarithm
- USACO 1.3 Prime Cryptarithm
- USACO 1.3-Prime Cryptarithm
- 深度对抗学习在图像分割和超分辨率中的应用
- A sequence of numbers (入门题-数学)
- CS107-Lecture 6-Note
- iReport专题学习之字段、参数04
- dovecot+mysql 空壳邮件
- USACO-Section 1.3 Prime Cryptarithm[...]
- ibatis sql_Map中出现异常:Cause: java.lang.RuntimeException: JavaBeansDataExchange could not instantiate..
- [区间dp] Dire Wolf HDU
- 07 lds文件与自定义段
- JS快速排序
- Android 进程间、线程间通讯方式总结
- Android Studio 升级到2.3版本后Run项目不能自动启动APP的问题
- hibernate saveorupdate方法只有更新有效果,保存没有效果
- iReport专题学习之变量、栏05