您的位置:首页 > 其它

猜数字游戏的提示(Master-Mind Hints, UVa 340)

2017-02-14 09:50 429 查看
       牵扯到一点逻辑...

       实现一个经典"猜数字"游戏。 给定答案序列和用户猜的序列,统计有多少数字位置正确

(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 MAX_NUM 1050

int main(void)
{
int n ;
int i_case = 0 ;
int a[MAX_NUM] , b[MAX_NUM];

freopen("input.txt" , "r" , stdin);
freopen("output.txt" , "w" , stdout);

while( scanf("%d" , &n) != EOF && n != 0)
{
int i ;
printf("Game:%d:\n" , i_case);

//存放a
for( i = 0 ; i < n ; i++)
{
scanf("%d" , &a[i]);
}

while(1)
{
int i = 0 , j ;
int A = 0 , B = 0;

//存放b,同时判断位置相同的数值是否相同
for( i = 0 ; i < n ; i++)
{
scanf("%d" , &b[i]);
if(a[i] == b[i])
{
A ++ ;
}
}

if( b[0] == 0) break;

for( i = 1 ; i <= 9 ; i++)
{
int count_a = 0 , count_b = 0;
for( j = 0 ; j < n ; j ++)
{
if( a[j] == i)
{
count_a ++ ;
}

if( b[j] == i)
{
count_b ++ ;
}
}

if(count_a > count_b) count_a = count_b ;
B+= count_a ;
}

printf(" (%d,%d)\n" , A , B-A);
}
i_case ++ ;
}

return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: