猜数字游戏的提示(UVa340)
题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=276
C++11代码如下:
1 #include<iostream> 2 using namespace std; 3 #define maxn 1010 4 int main() { 5 int n; 6 int a[maxn], b[maxn]; 7 int count = 0; 8 while ((cin >> n) && n) { 9 cout << "Game " << ++count << ':' << endl; 10 for (int i = 0; i < n; i++) cin >> a[i]; 11 for (;;) { 12 int A = 0, B = 0; 13 for (int i = 0; i < n; i++) { 14 cin >> b[i]; 15 if (a[i] == b[i]) A++; 16 } 17 if (b[0] == 0) break; 18 for (int d = 1; d < 10; d++) { //统计每个数字在数组a、b中的出现次数 19 int c1 = 0, c2 = 0; 20 for (int i = 0; i < n; i++) { 21 if (a[i] == d) c1++; 22 if (b[i] == d) c2++; 23 } 24 (c1 < c2) ? (B += c1) : (B += c2); //取小者计入B,a、b中未同时出现的不会计入,位置正确的(A)也会计入 25 } 26 cout << " (" << A << ',' << B - A << ')' << endl; //B-A即为都出现且位置不对 27 } 28 } 29 return 0; 30 }