NYOJ 286 动物统计

本文介绍了一种简单的字符串统计方法,并通过实例演示了如何使用排序算法对字符串进行排序。

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

题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=286

简单的字符串统计,不多说,直接水过。

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct tj
{
	char str[11];
	int count;
}w[10001];
bool comp(tj x,tj y)
{
	if(strcmp(x.str,y.str)<0)//**将字符串按字典序从小到大排序**//
		return true;
	return false;
}
int main()
{
	int n,i,j,p,max;
	memset(w,0,sizeof(w));
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%s",w[i].str);
		w[i].count=1;
	}
	sort(w,w+n,comp);
	p=0;max=0;//**用p代表指向最多数量的字符串**//
	for(i=1;i<n;i++)
	{
		if(strcmp(w[i].str,w[i-1].str)==0)//**后一个与前一个相等**//
		{
			w[i].count=w[i-1].count+1;
		}
		if(w[i].count>max)
		{
			max=w[i].count;
			p=i;
		}
	}
	printf("%s %d",w[p].str,max);
	printf("\n");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值