你的任务是实现一个经典的“猜数字”游戏。给定答案序列和用户猜的序列,统计有多少数字位置正确(设为AA ),有多少数字在两个序列中都出现过但位置不对(BB )。
输入包含多组数据。每组输入第一行为序列长度nn ,第二行是答案序列,接下来是若干行猜测序列。猜测序列全00 时表示该组数据结束。n=0n=0 时输入结束。
对于每一组数据,输出的开头应有一行**“Game x:”(没有双引号,x为当前组数据的编号,从1开始递增),然后对于每个猜测序列,输出一组数,格式(A,B),A,B的意义如上所示,注意在(A,B)**之前要输出四个空格符。
输入输出样例
输入 #1 复制
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
输出 #1 复制
Game 1:
(1,1)
(2,0)
(1,2)
(1,2)
(4,0)
Game 2:
(2,4)
(3,2)
(5,0)
(7,0)
分析:(A,B)中A值为与答案序列对应的位置正确的数字出现次数,设定两个序列中出现相同数字的次数为C,B=C-A;
#include<cstdio>
#include<iostream>
using namespace std;
const int N = 1000 +10 ;
int main(){
int n, a[N],b[N];
int kase = 0;
while(scanf("%d",&n) == 1 && n){
printf("Game %d:\n", ++kase);
for(int i = 0; i< n; i++)
scanf("%d",&a[i]);
for(;;){
int A = 0, B = 0;
for(int i = 0;i < n;i++)
{
scanf("%d", &b[i]);
if(a[i] == b[i])
A++;
}
if(b[0] == 0) 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 0;
}