PAT B1042 character statistics (20point (s))

Please write a program to identify the most frequent letters that appear in the text for a given period.

Input formats:

Enter a given length of string in a row does not exceed 1000. String of ASCII code table any visible characters and spaces composition containing at least one English letter, end with a carriage return (Enter not count).

Output formats:

The maximum output frequency of the letters appear and the number of occurrences in a row, separated by a space between them. If there side by side, the output alphabetically minimum that letter. Case insensitive statistics, the output of lower-case letters.

Sample input:

This is a simple TEST.  There ARE numbers and other symbols 1&2&3...........

Sample output:

e 7
  • Ideas: Hash
    details: Direct find the maximum letter k

  • code:

#include <bits/stdc++.h>
using namespace std;
int has[256];
int main(){
	string s;
	getline(cin, s);
	for(int i = 0; i < s.size(); ++i){
		if(islower(s[i])) has[s[i]]++;
		if(isupper(s[i])) has[tolower(s[i])]++;
	}
	int k = 'a'; 
	for(int i = 'a'; i <= 'z'; ++i){
		if(has[i] > has[k]){
			k = i; 
		}
	}
	printf("%c %d", k, has[k]);
	return 0;
} 
Published 243 original articles · won praise 4 · Views 4060

Guess you like

Origin blog.csdn.net/qq_42347617/article/details/104045611