其他分享
首页 > 其他分享> > 【UJN-寒假练练练】新年快乐-2-进制转换与字符串_S - Number of letters

【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]=='\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 %d\n",ch,map[pos]);
	}
	
    return 0;
}

个人见解 酌情采纳.

标签:count,新年快乐,letters,string,int,Number,UJN,Output
来源: https://blog.csdn.net/qq_63173957/article/details/122394508