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;