UVA-340 Master-Mind Hints
题目大意:designer 说出一组数字, breaker 也说出一组数字(不包括 0 ,所以 0 可以当成标记),如果designer 与 breaker 的位置和数字均相等,则 strong++ ,反之则 weak++。 且参与比较过的数字不再参与后面的比较。
Sample Input
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
Sample Output
Game 1:
(1,1)
(2,0)
(1,2)
(1,2)
(4,0)
Game 2:
(2,4)
(3,2)
(5,0)
(7,0)
解题思路:先比较 strong , 比较后要标记一下,再比较 weak 。
//UVA-340 Master-Mind Hints
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define m 5007
int T = 1;
int designer[m] = { 0 };
int breaker[m] = { 1 };
int design[m] ={ 0 };
int main () {
int M;
while(scanf("%d",&M) && M != 0) {
for(int i = 0; i < M; i++) {
scanf("%d",&design[i]);
}
printf ("Game %d:\n",T++);
for (int j = 0; j < M; j++) {
scanf ("%d",&breaker[j]);
}
while (breaker[0] != 0) {
int strong = 0, weak = 0;
memcpy(designer,design,m);
for (int i = 0; i < M;i++) {
if (designer[i] == breaker[i]) {
strong++;
breaker[i] = designer[i] = 0; //此处两个都是i
}
}
for (int i = 0; i < M; i++) {
if (designer[i] == 0)
continue;
for (int j = 0; j < M; j++) {
if (designer[i] == breaker[j]) {
weak++;
designer[i] = breaker[j] = 0; //此处两个分别为 i 和 j.
break;
}
}
}
printf (" (%d,%d)\n",strong,weak);
for (int j = 0; j < M; j++) {
scanf ("%d",&breaker[j]);
}
}
}
return 0;
}