例题3-4 UVA340 猜数字游戏的提示 Master-Mind Hints

这道题读起来感觉莫名其妙,要是只看书上的中文翻译,估计是看不懂怎么回事,要输出的第二个数是啥意思,看了看刘汝佳的解释,很快就写出来了,英语还是很重要。

#include <bits/stdc++.h>

using namespace std;

int main() {
    int n;
    int t = 1;
    while (cin >> n && n) {
        cout << "Game " << t++ << ":\n";
        int a[1005];
        int hash1[10] = {};
        for (int i = 0; i < n; i++) {
            cin >> a[i];
            hash1[a[i]]++;
        }
        while (1) {
            int ok1 = 0;
            int b[1005];
            int ans1 = 0;
            int hash2[10] = {};
            for (int i = 0; i < n; i++) {
                cin >> b[i];
                hash2[b[i]]++;
                if (b[i]) ok1 = 1;
                if (b[i] == a[i]) ans1++;
            }
            if (!ok1) break;
            int temp = 0;
            for (int i = 1; i <= 9; i++) {
                temp += min(hash1[i], hash2[i]);
            }
            cout << "    (" << ans1 << "," << temp - ans1 << ')' << endl;
        }
    }
    return 0;
}

版权声明:本文为weixin_46709784原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。