练习codeforces1003A. Polycarp's Pockets

题目如下
A. Polycarp's Pockets
time limit per test1 second
memory limit per test256 megabytes
Polycarp has 𝑛 coins, the value of the 𝑖-th coin is 𝑎𝑖. Polycarp wants to distribute all the coins between his pockets, but he cannot put two coins with the same value into the same pocket.

For example, if Polycarp has got six coins represented as an array 𝑎=[1,2,4,3,3,2], he can distribute the coins into two pockets as follows: [1,2,3],[2,3,4].

Polycarp wants to distribute all the coins with the minimum number of used pockets. Help him to do that.

Input
The first line of the input contains one integer 𝑛 (1≤𝑛≤100) — the number of coins.

The second line of the input contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤100) — values of coins.

Output
Print only one integer — the minimum number of pockets Polycarp needs to distribute all the coins so no two coins with the same value are put into the same pocket.

题目大意
现有n个硬币,要求将他们分到几个口袋中,要求是面值相同的不能放入同一口袋中,也就是同一口袋中硬币的面值都不相同,问最少需要多少口袋可以分完这对硬币

题目分析
也就是说无论这里面有多少相同面值的硬币,我们只需要找到相同面值数量最多的硬币的数量即可,也就是说找出一个数组中的众数的个数
实现
我们通过map数组来统计每个面值的硬币的数量,然后再比较找出数量最大的并输出
完整代码

点击查看代码
include <stdio.h>
#include <algorithm>
#include <map>
using namespace std;
int main(){
    int n;
    scanf("%d",&n);
    int coins[101];
    map<int, int> freq;
    for(int i = 0; i < n; i++){
        scanf(" %d", &coins[i]);
        freq[coins[i]]++;
    }
    int max = 0;
    for(int i = 0; i < n; i++){
        if(freq[coins[i]] > max){
            max = freq[coins[i]];
        }
    }
    printf("%d",max);
}
posted @ 2025-07-07 20:28  sirro1uta  阅读(12)  评论(0)    收藏  举报