78.分组飞行棋棋子<字节青训营-简单题>

1.题目

问题描述

现在桌子上有一堆飞行棋棋子,有 N 个,每个棋子上标有数字序号,现在想让你帮忙给这堆飞行棋分成 M 组,需要满足:

  • 每个分组只能包含 5 个棋子
  • 每个棋子只能出现在一个分组里
  • 每个分组里的棋子的数字序号相同

请问可以完成上述分组么?

输入格式

空格分割的飞行棋棋子序号,如:1 3 4 5 6 5 4

输出格式

是否可以完成分组,如果可以输出 true,否则输出 false

输入样例(1)

1 2 3 4 5

上述棋子只有 5 个只能分为一组,但组内棋子序号不一致,所以无法完成分组,输出 false

输出样例(2)

1 1 1 1 2 1 2 2 2 2

上述棋子可以分为两组,[1, 1, 1, 1, 1] 和 [2, 2, 2, 2, 2] 两组,可以完成分组,输出 true

数据范围

  • 棋子数量:1 <= N <= 10^5
  • 棋子序号:1 <= pieces[i] <= 40

2.思路

对出现的每个序号的棋子进行计数,如果每种棋子的个数都是5的倍数,则可以完成分组

3.代码

#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

std::string solution(std::vector<int> nums) {
    // Please write your code here
    map<int, int> cnts;
    for (int i = 0; i < nums.size(); i++){
        cnts[nums[i]] += 1;
    }
    for (auto &pair : cnts){
        if (pair.second != 5){
            cout << pair.second << endl;
            return "False";
        }
    }
    return "True";
}

int main() {
    // You can add more test cases here
    std::cout << solution({1, 3, 4, 5, 6, 5, 4});
    std::cout << (solution({1, 3, 4, 5, 6, 5, 4}) == "False") << std::endl;
    std::cout << (solution({1, 1, 1, 1, 2, 1, 2, 2, 2, 2}) == "True") << std::endl;
    std::cout << (solution({11, 45, 49, 37, 45, 38, 3, 47, 35, 49, 26, 16, 24, 4, 45, 39, 28, 26, 14, 22, 4, 49, 18, 4, 4, 26, 47, 14, 1, 21, 9, 26, 17, 12, 44, 28, 24, 24, 10, 31, 33, 32, 23, 41, 41, 19, 17, 24, 28, 46, 28, 4, 18, 23, 48, 45, 7, 21, 12, 40, 2, 19, 19, 28, 32, 6, 27, 43, 6, 18, 8, 27, 9, 6, 6, 31, 37, 15, 26, 20, 43, 3, 14, 40, 20}) == "False") << std::endl;
    return 0;
}

第三个测试样例错误:

输入 nums = [5,5,5,5,5,5,5,5,5,5] 输出 你的输出=“False" 预期输出=“True"

应将pair.second != 5,改为pair.second % 5 ≠ 0,表示个数不是5的倍数

#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;

std::string solution(std::vector<int> nums) {
    // Please write your code here
    map<int, int> cnts;
    for (int i = 0; i < nums.size(); i++){
        cnts[nums[i]] += 1;
    }
    for (auto &pair : cnts){
        if (pair.second % 5 !=  0){
            cout << pair.second << endl;
            return "False";
        }
    }
    return "True";
}

int main() {
    // You can add more test cases here
    std::cout << solution({1, 3, 4, 5, 6, 5, 4});
    std::cout << (solution({1, 3, 4, 5, 6, 5, 4}) == "False") << std::endl;
    std::cout << (solution({1, 1, 1, 1, 2, 1, 2, 2, 2, 2}) == "True") << std::endl;
    std::cout << (solution({11, 45, 49, 37, 45, 38, 3, 47, 35, 49, 26, 16, 24, 4, 45, 39, 28, 26, 14, 22, 4, 49, 18, 4, 4, 26, 47, 14, 1, 21, 9, 26, 17, 12, 44, 28, 24, 24, 10, 31, 33, 32, 23, 41, 41, 19, 17, 24, 28, 46, 28, 4, 18, 23, 48, 45, 7, 21, 12, 40, 2, 19, 19, 28, 32, 6, 27, 43, 6, 18, 8, 27, 9, 6, 6, 31, 37, 15, 26, 20, 43, 3, 14, 40, 20}) == "False") << std::endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值