前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ACMSGURU 486 - Bulls and Cows

ACMSGURU 486 - Bulls and Cows

作者头像
Reck Zhang
发布2021-08-11 11:12:03
3480
发布2021-08-11 11:12:03
举报
文章被收录于专栏:Reck Zhang

Bulls and Cows

Problem Description

You probably know the game “bulls and cows”. Just in case, we explain the rules. The first player picks a four-digit number with all digits distinct (leading zero is allowed) and keeps it secret. The second player tries to guess the secret number. For each guess, the first player issues a response in the form “n bulls, m cows”. A “bull” is a digit that is present in both the secret and the guess and occurs in the same position in both. A “cow” is a digit that is present in both numbers, but occurs in different positions.

For example, if the first player picked 5071, and the second guessed 6012, the response would be “one bull, one cow”. Here the “bull” is the digit 0, as it is in the second position in both numbers, and the “cow” is the digit 1, as it is in the fourth position in the secret, but in the third position in the guess.

Write a program to count the number of cows and bulls for the given the secret and guess.

Input

The first line of the input file contains four digits, the number picked by the first player. The second line contains the number guessed by the second player in the same format.

Output

The first and only line of the output file should contain two integers separated by a space, the number of “bulls” and the number of “cows”.

Example(s)

sample input

sample output

50716012

1 1

sample input

sample output

43214321

4 0

sample input

sample output

19800879

0 3

sample input

sample output

12345678

0 0

Solution

代码语言:javascript
复制
#include <bits/stdc++.h>

int main() {
    std::ios::sync_with_stdio(false);

    std::string a{};
    std::string b{};

    std::cin >> a >> b;
    std::vector<int> countA = std::vector<int>(10, 0);
    std::vector<int> countB = std::vector<int>(10, 0);

    int bulls = 0;
    int cows = 0;
    for(int i = 0; i < 4; i++) {
        countA[a[i] - '0'] += 1;
        countB[b[i] - '0'] += 1;
        if(a[i] == b[i]) {
            bulls += 1;
        }
    }

    for(int i = 0; i < 10; i++) {
        if(countA[i] != 0 && countB[i] != 0) {
            cows += std::min(countA[i], countB[i]);
        }
    }

    std::cout << bulls << " " << cows - bulls << std::endl;
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-10-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Bulls and Cows
    • Problem Description
      • Input
        • Output
          • Example(s)
            • Solution
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档