一、题目
实现一个经典"猜数字"游戏。给定答案序列和用户猜的序列,统计有多少数字位置正确(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);因为当他们位置相同 min为1 ,所以得出结果减去A。
就得到B。
三 、代码如下
- #include <iostream>
- #include<stdio.h>
- #include <string.h>
- using namespace std;
- int a[100],b[100];
- int main(int argc, char** argv) {
- int n; // n表示数组长度
- while((scanf("%d",&n))==1&&n) { //输入的N不为零
- int count=0;
- for(int i=0;i<n;i++)
- scanf("%d",&a[i]);
- while(count!=n) { //判断是否结束
- count=0;
- int x=0,y=0; //x代表A ,y代表B
- for(int i=0;i<n;i++)
- {scanf("%d",&b[i]);
- if(b[i]==0)
- count++;
- if(b[i]==a[i]) //得出A
- {x++;
- }
- }
- for(int i=1;i<10;i++) { //得出结果B
- int pp=0,ppp=0;
- for(int j=0;j<n;j++) {
- if (a[j]==i) pp++;
- if(b[j]==i) ppp++;
- }
- if(ppp>pp) y=y+pp;
- else y=y+ppp;
- }
- if(count==n)
- printf("get over");
- else
- printf("%d %d",x,y-x);
- }
- }
- return 0;
- }