欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

UVA - 340 Master-Mind Hints

程序员文章站 2024-02-24 23:16:16
...
//例题3-4 猜数字游戏的提示
//收获:处理思路:在猜测和答案中都出现某数字,且在猜测和答案位置不同的次数 = 都出现的次数 -  数字位置正确的次数; 都出现的次数 = min(答案中出现次数,猜测串中出现次数) 

#include <iostream>
using namespace std;
int main()
{
	int n, *a, *b;
	int sum1, sum2, c1, c2, kase = 0;
	while (cin >> n)
	{
		if (!n) break;
		a = new int [n];
		b = new int [n];
		
		cout << "Game " << ++kase << ":" << endl;
		for (int i = 0; i < n; i++) cin >> a[i];
		while(1)
		{
			sum1 = sum2 = 0;
			for (int i = 0; i < n; i++)
			{
				cin >> b[i];
				if (a[i] == b[i]) sum1++;
			}
			if (b[0] == 0) break;
			
			for (int j = 1; j <= 9; j++)
			{
				c1 = c2 = 0;
				for (int i = 0; i < n; i++)
				{
					if (a[i] == j) c1++;
					if (b[i] == j) c2++;
				}
				sum2 += min(c1, c2);
			}
			cout << "    (" << sum1 << "," << sum2 - sum1 << ")" << endl;
		}
	}
	delete[]a;
	delete[]b;
	return 0;
}