Problem I 猜數字遊戲的提示(第二講)

題目描述
實現一個經典"猜數字"遊戲。 給定答案序列和用戶猜的序列,統計有多少數字位置正確(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)

 

 

#include<stdio.h>
#define MAXN 1010
int main()
{
    int a[MAXN], b[MAXN];
    int q = 0;
    while(1)
	{
		int n;
		scanf("%d", &n);
		if(n==0) break;

		q++;
		printf("Game %d:\n", q);
		int i;
		for(i = 0; i < n; ++i)
			scanf("%d", &a[i]);
		for(;;)
		{
			
			for(i = 0; i < n; ++i)
			{
				scanf("%d", &b[i]);				
			}

			int count=0;
			for(i=0;i<n;i++)
			{
				if(b[i]!=0)
					count++;
			}
				
			if(count == 0) 
				break;
				
			int x=0;
			for(i = 0; i < n; ++i)
			{				
				if(a[i] == b[i]) x++;
			}

			int j ;
			int y=0;
			for(j = 0; j <= 9; ++j) 
			{
				int c1 = 0, c2 = 0;
				for(i = 0; i < n; ++i)
				{
					if(a[i] == j) ++c1;
					if(b[i] == j) ++c2;
				}
				if(c1 < c2) 
					y = y + c1;
				else 
					y = y + c2;
			}
            printf("(%d,%d)\n", x, y-x);
			
		}
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章