我是靠谱客的博主 鲤鱼犀牛,最近开发中收集的这篇文章主要介绍【UJN-寒假练练练】新年快乐-2-进制转换与字符串_S - Number of letters,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

S - Number of letters

Find out the letter that occurs the most times in a string that consistes
of 'a'-'z'.

Input

Line 1: an integer N, indicates the number of test case

Line 2, 4, 6...2N: a string consistes of 'a'-'z', of which the size is from 1 to 1000 inclusive

LIne 3, 5, 7...2N-1: blank line

Output

Output N lines corresponding N strings of inputs.

Each line contains a letter that occurs the most times in the corresponding string and a number that is the time of occurence, seperated by a blank space.

Sample Input

2
abbccc

adfadffasdf

Sample Output

c 3
f 4

题意:

找到对应字符串中出现次数最多的小写字母

题解:

01 map[26]={"abcdefghijklmnopqrstuvwxyz"}; 进行映射

02 (不用映射也行)用下标做一个加法

03 小心输入字符串时中间的空行

//  S - Number of letters
#include<stdio.h>
#include<string.h>

int main()
{
	int n,i,count,pos;
	int map[26];
	char str[1005],ch;
	
	// 老规矩 %*c 吃回车 
	scanf("%d%*c",&n);
	
	count=0;
	
	while( count<n )
	{
		gets( str );
		
		// 跳过空行 
		if( str[0]=='' ) continue;
		
		count++;
		
		memset( map,0,sizeof( map ) );
		
		// 计数 
		for( i=0;str[i];i++ )
		{
			map[ str[i]-'a' ]++;
		}
		
		pos=0;
		ch='a';
		
		// 找出现次数最多的小写字母 
		for( i=1;i<26;i++ )
		{
			if( map[pos]<map[i] )
			{
				// 用下标做加法 也能找到对应小写字母 
				pos=i;
				ch='a'+i;
			}
		}
		
		printf("%c %dn",ch,map[pos]);
	}
	
    return 0;
}

个人见解 酌情采纳.

最后

以上就是鲤鱼犀牛为你收集整理的【UJN-寒假练练练】新年快乐-2-进制转换与字符串_S - Number of letters的全部内容,希望文章能够帮你解决【UJN-寒假练练练】新年快乐-2-进制转换与字符串_S - Number of letters所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(74)

评论列表共有 0 条评论

立即
投稿
返回
顶部