猜数字游戏的提示(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 }

原文链接: https://www.cnblogs.com/pgzhang/p/9224802.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    猜数字游戏的提示(UVa340)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/276629

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 上午1:53
下一篇 2023年2月15日 上午1:53

相关推荐