链接::​​click here​

题意:给出许多动物的数量,判断这片森林中哪种动物的数量最多,CSDN博客第一篇,纪念一下。

思路:

         简单的字符串判断题。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int b,i,j,max=0,ans=1;
string q[10001],name;
cin>>b;
for(i=0; i<b; i++)
cin>>q[i];
sort(q,q+b);
for(j=0; j<b-1; j++)
{
if(q[j+1]==q[j])
ans++;
else
ans=1;
if(max<ans)
{
max=ans;
name=q[j];
}
}
cout<<name<<" "<<max<<endl;
return 0;
}

另一种写法:

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct xin
{
char a[12];
}r[12345];
bool cmp(xin x, xin y)
{
return strcmp(x.a,y.a)<0;
}
int main()
{

int n,i,max=-1,count=1;
char c[12];
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",r[i].a);
sort(r,r+n,cmp);
strcpy(c,r[0].a);
for(i=0;i<n-1;i++)
{
if(strcmp(r[i].a,r[i+1].a)==0)
{
count++;
if(count>max)
{
max=count;
strcpy(c,r[i].a);
}
}
else count=1;
}
printf("%s %d\n",c,max);

}