题解 | #Polycarp's Pockets#

Polycarps Pockets

https://ac.nowcoder.com/acm/problem/112909

题目描述

Polycarp has nnn coins, the value of the iii-th coin is aia_iai. 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 a=[1,2,4,3,3,2]a = [1, 2, 4, 3, 3, 2]a=[1,2,4,3,3,2], he can distribute the coins into two pockets as follows: [1,2,3],[2,3,4][1, 2, 3], [2, 3, 4][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.

输入描述:

The first line of the input contains one integer nnn (1≤n≤1001 \le n \le 1001n100) — the number of coins.

The second line of the input contains nnn integers a1,a2,…,ana_1, a_2, \dots, a_na1,a2,,an (1≤ai≤1001 \le a_i \le 1001ai100) — values of coins.

输出描述:

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不超过100,a也不超过100,直接用数组干吧
a数组表示要输入的n个数,sum数组表示每个数出现的次数(数就是下标)
要求一个口袋中不能有两枚相同硬币,则需要的口袋数即是n个数中出现次数最多的数所对应的次数,即sum的最大值
代码:
#include<iostream>
using namespace std;
int main()
{
    int n, * a;
    cin >> n;
    a = new int[n];
    int max = 0;
    int sum[105] = { 0 };
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        sum[a[i]]++;
        if (max < sum[a[i]]) max = sum[a[i]];
    }
    cout << max;
    return 0;
}


全部评论

相关推荐

点赞 评论 收藏
分享
斑驳不同:还为啥暴躁 假的不骂你骂谁啊
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务