猜数字游戏的提示(UVa340)

时间:2023-03-10 06:00:07
猜数字游戏的提示(UVa340)

  题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=276

C++11代码如下:

 #include<iostream>
using namespace std;
#define maxn 1010
int main() {
int n;
int a[maxn], b[maxn];
int count = ;
while ((cin >> n) && n) {
cout << "Game " << ++count << ':' << endl;
for (int i = ; i < n; i++) cin >> a[i];
for (;;) {
int A = , B = ;
for (int i = ; i < n; i++) {
cin >> b[i];
if (a[i] == b[i]) A++;
}
if (b[] == ) break;
for (int d = ; d < ; d++) { //统计每个数字在数组a、b中的出现次数
int c1 = , c2 = ;
for (int i = ; i < n; i++) {
if (a[i] == d) c1++;
if (b[i] == d) c2++;
}
(c1 < c2) ? (B += c1) : (B += c2); //取小者计入B,a、b中未同时出现的不会计入,位置正确的(A)也会计入
}
cout << " (" << A << ',' << B - A << ')' << endl; //B-A即为都出现且位置不对
}
}
return ;
}