暑假练习赛1——Problem A(水题,统计个数最多的硬币)

本文解析了Codeforces上的一道A级题目,介绍了如何通过统计硬币的不同价值来确定最少的打包数量,避免同一价值的硬币放入同一包裹。文章对比了两种解题思路,并给出了AC代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

cf题目链接:http://codeforces.com/problemset/problem/1003/A

 

Problem A

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other)

Total Submission(s) : 25   Accepted Submission(s) : 0

Problem Description

Codeforces (c) Copyright 2010-2018 Mike Mirzayanov

 

 

Input

<p>The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) the number of coins.</p><p>The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 100$$$) values of coins.</p>

 

 

Output

<p>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.</p>

 

 

Sample Input

 

6 1 2 4 3 3 2 1 100

 

 

Sample Output

 

2 1

======================================

题意:有n枚硬币,每一枚硬币有一权值,现将所有硬币打包,每个包中硬币的权值各不相同,问最少需要几个包。

思路:找个数最多的硬币即可。

问题:统计硬币数目的代码实现想复杂了,正确思路:统计各种硬币数目用一个数组++记录即可。

AC代码(想复杂的):

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int a[10010];
int main()
{
    int n;

    while(cin>>n)
    {
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;++i)cin>>a[i];
        int t2;
        sort(a+1,a+1+n);
        t2=-999999;
        int max=-99999999,k=0;
        for(int i=1;i<=n;++i)
        {
                t2=a[i];
                while(a[i]==t2)i++,k++;
                if(k>max)max=k;
                i--;
                k=0;

        }
        cout<<max<<endl;
    }
    return 0;
}

 

修改AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int a[10010];
int main()
{
    int n;

    while(cin>>n)
    {
        memset(a,0,sizeof(a));
        int ans=-999999999;
        for(int i=1;i<=n;++i)
        {
            int x;
            cin>>x;
            a[x]++;
            ans=max(ans,a[x]);
        }
        cout<<ans<<endl;
    }
    return 0;
}

 

The end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值