猜数字游戏的提示(Master-Mind Hints, UVa 340)
2024-10-01 00:19:49
实现一个经典"猜数字"游戏。
给定答案序列和用户猜的序列,统计有多少数字位置正确 (A),有多少数字在两个序列都出现过但位置不对(B)。
输入包含多组数据。每组输入第一行为序列长度n,第二行是答案序列,接下来是若干猜测序列。猜测序列全0时该组数据结束。n=0时输入结束。
样例输入:
4
1 3 5 5
1 1 2 3
4 3 3 5
6 5 5 1
6 1 3 5
1 3 5 5
0 0 0 0
10
1 2 2 2 4 5 6 6 6 9
1 2 3 4 5 6 7 8 9 1
1 1 2 2 3 3 4 4 5 5
1 2 1 3 1 5 1 6 1 9
1 2 2 5 5 5 6 6 6 7
0 0 0 0 0 0 0 0 0 0
0
样例输出:
Game 1:
(1,1)
(2,0)
(1,2)
(1,2)
(4,0)
Game 2:
(2,4)
(3,2)
(5,0)
(7,0)
【分析】
直接统计可得A,为了求B,对于每个数字(1~9),统计二者出现的次数c1和c2,则 min(c1,c2)就是该数字对B的贡献。最后要减去A的部分。
#include<cstdio>
#define maxn 1010
using namespace std;
int a[maxn],b[maxn];
int main(){
int count=;int n;
while(scanf("%d",&n)==&&n){//n=0时输入结束
printf("Game %d:\n", ++count);
for(int i=;i<n;i++) scanf("%d",&a[i]);
while(){
int A=,B=;
for(int i=;i<n;i++) {
scanf("%d",&b[i]);
if(a[i] == b[i]) A++;
}
if(b[]==)break;//正常的猜测序列不会有0,所以只判断第一个数是否为0即可
for(int d = 1; d <= 9; d++) {
int c1 = 0, c2 = 0; //统计数字d在答案序列和猜测序列中各出现多少次
for(int i = 0; i < n; i++) {
if(a[i] == d) c1++;
if(b[i] == d) c2++;
}
if(c1 < c2) B += c1;
else B += c2;
}
printf(" (%d,%d)\n", A, B-A);
}
}
return ;
}
最新文章
- easyui-datagrid 列单击事件
- MySQL时间戳相互转换
- poj 3321:Apple Tree(树状数组,提高题)
- MDX语法之排序函数Order
- Android总结篇系列:Activity生命周期
- HBase数据模型剖析
- paper 59:招聘
- 打开office弹出steup error 的解决办法
- C#下利用高精度计时器进行计时操作
- openstack 云平台API
- LA 6856 Circle of digits 解题报告
- android .9.png ”点九” 图片制作方法
- Spring Bean装配
- 《深入探索Androdi热修复技术原理(阿里巴巴)》--读书笔记
- Spring事务<;tx:annotation-driven/>;的理解
- [日常] Go语言圣经--包和文件-导入包习题
- Java 多线程(三)之线程状态及其验证
- 监听home键+模拟home键
- Android的 EditText的inputType类型
- PowerDesigner学习笔记
热门文章
- iOS开发之剖析&;quot;秘密&;quot;App内容页面效果(一)
- Powershell远程在Azure A7虚拟机执行Java JVM失败
- hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
- 用R语言完成的交通可视化报告
- 20170623_oracle_SQL
- 理解SetCapture、ReleaseCapture、GetCapture(控制了消息发往哪个窗口,即消息窗口)
- linux常用命令---持续添加中...
- poj 2104 K-th Number 主席树+超级详细解释
- jsp整合discuz
- 汇编程序45:检测点13.2 (loop指令的中断例程)