UVa340

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=276

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n; // the length of password
int kase = 1;
while(cin >> n){
if(n == 0) break;
cout << "Game " << kase << ":\n";

vector<int> password(n), guess(n);
int size = password.size();
// input password
for(int i=0; i<size; ++i){
cin >> password[i];
}

while(true){
// check if guess is all 0
bool allZero = true;
for(int i = 0; i<size; ++i){
cin >> guess[i];
if(guess[i] != 0) allZero = false;
}
if(allZero) break;

vector<int> tmp = password; // use tmp instead of password for easier modification

int a=0, b=0; // a for correct pos, b for correct int
for(int i=0; i<size; ++i){
// if found at correct pos, ++a, change it to -1
if(tmp[i] == guess[i]){
++a;
tmp[i] = -1;
guess[i] = -1;
}
}

// use maps to check how many times it shows up
map<int, int> mp; // map password (use tmp to implement)
map<int, int> mg; // map guess
for(int i=0; i<size; ++i){
if(tmp[i] != -1) ++mp[tmp[i]];
if(guess[i] != -1) ++mg[guess[i]];
}
for(int i=1; i<10; ++i){
b += min(mp[i], mg[i]);
}

cout << " (" << a << ',' << b << ")\n";
}
++kase;
}
return 0;
}