UVa 340 - Master-Mind Hints
来源:互联网 发布:java android api 编辑:程序博客网 时间:2024/06/05 22:52
题目链接:点击打开链接
解题思路:
设数字位置正确的有a个,数字相同但位置不同的有b个。
假设答案序列中有5个1,输入的序列中有4个1,其中我们知道数字位置正确的有1个,那么对于1来说,数字相同但位置不同的有多少个呢?
答案是3个,想一想。
关键在于这个思想,其中对于1来说,b = min(5,4)-a = min(5,4) - 1 = 5。
那么因为输入序列是1到9的数字,我们只需要统计1到9里面,对于答案序列和猜测序列中,1出现的最少次数,2出现的最少次数,3出现的最少次数,……。因为统计的时候一定包含数字位置正确的个数,所以最后的结果再减去数字位置正确的个数即可。
代码:
#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <ctype.h>using namespace std;int n;int main(){ int cnt = 0; while(scanf("%d", &n) == 1 && n) { int *a = new int[n+1]; int *b = new int[n+1]; printf("Game %d:\n", ++cnt); for(int i = 0; i < n; i++) scanf("%d", &a[i]); while(1) { int c1, c2; c1 = c2 = 0; int ans1, ans2; ans1 = ans2 = 0; for(int i = 0; i < n; i++) { scanf("%d", &b[i]); if(b[i] == a[i]) ans1++; } if(b[0] == 0) break; for(int i = 1; i <= 9; i++) { c1 = c2 = 0; for(int j = 0; j < n; j++) { if(a[j] == i) c1++; if(b[j] == i) c2++; } ans2 += min(c1, c2); } printf(" (%d,%d)\n", ans1, ans2-ans1); } } return 0;}
0 0
- UVa 340 Master-Mind Hints
- uva 340 Master-Mind Hints
- UVA 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- UVa 340 Master-Mind Hints
- UVA 340 Master-Mind Hints
- uva 340 Master-Mind Hints
- UVA 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- UVA 340 Master-Mind Hints
- uva 340 Master-Mind Hints
- UVa - 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- uva 340 Master-Mind Hints
- uva - 340 - Master-Mind Hints
- UVa 340 - Master-Mind Hints
- UVa:340 - Master-Mind Hints
- HDU 5654 xiaoxin and his watermelon candy
- 关于大型网站技术演进的思考(十四)--网站静态化处理—前后端分离—上(6)
- 挖掘DBLP作者合作关系,FP-Growth算法实践(5):挖掘研究者合作关系
- 文档词频分析小工具
- 关于大型网站技术演进的思考(十三)--网站静态化处理—CSI(5)
- UVa 340 - Master-Mind Hints
- 笔试练习二
- 有关Mac版本AS类里面无法编写代码的问题
- MYSQL中group_concat有长度限制!默认1024
- 微信手机网页上传图片高效率压缩(Canvas+Base64)
- Java中的native方法
- 《深入理解java虚拟机》读书笔记:Java对象的内存布局
- MarkDown使用说明
- 山东省第一届ACM大学生程序设计竞赛 Shopping 水。。。。。。。