实现一个经典"猜数字"游戏。

给定答案序列和用户猜的序列,统计有多少数字位置正确 (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 ;
}

最新文章

  1. easyui-datagrid 列单击事件
  2. MySQL时间戳相互转换
  3. poj 3321:Apple Tree(树状数组,提高题)
  4. MDX语法之排序函数Order
  5. Android总结篇系列:Activity生命周期
  6. HBase数据模型剖析
  7. paper 59:招聘
  8. 打开office弹出steup error 的解决办法
  9. C#下利用高精度计时器进行计时操作
  10. openstack 云平台API
  11. LA 6856 Circle of digits 解题报告
  12. android .9.png ”点九” 图片制作方法
  13. Spring Bean装配
  14. 《深入探索Androdi热修复技术原理(阿里巴巴)》--读书笔记
  15. Spring事务&lt;tx:annotation-driven/&gt;的理解
  16. [日常] Go语言圣经--包和文件-导入包习题
  17. Java 多线程(三)之线程状态及其验证
  18. 监听home键+模拟home键
  19. Android的 EditText的inputType类型
  20. PowerDesigner学习笔记

热门文章

  1. iOS开发之剖析&amp;quot;秘密&amp;quot;App内容页面效果(一)
  2. Powershell远程在Azure A7虚拟机执行Java JVM失败
  3. hdu 5416 CRB and Tree(2015 Multi-University Training Contest 10)
  4. 用R语言完成的交通可视化报告
  5. 20170623_oracle_SQL
  6. 理解SetCapture、ReleaseCapture、GetCapture(控制了消息发往哪个窗口,即消息窗口)
  7. linux常用命令---持续添加中...
  8. poj 2104 K-th Number 主席树+超级详细解释
  9. jsp整合discuz
  10. 汇编程序45:检测点13.2 (loop指令的中断例程)