PAT1042 字符统计 (20 分)

题目

在这里插入图片描述

代码

#include<iostream>
#include<string>
using namespace std;
int main()
{
	int count[26] = { 0 };
	char c;

	//输入并计算重复次数
	while (1)
	{
		c = getchar();

		if (c == '\n')//结束
		{
			break;
		}

		else if (c >= 'a'&&c <= 'z')
		{
			count[c - 'a']++;
		}
		else if (c >= 'A'&&c <= 'Z')
		{
			count[c - 'A']++;
		}
	}

	//找最大
	int maxIndex = 0;
	int i;
	for (i = 0; i < 26; i++)
	{
		if (count[i] > count[maxIndex])
		{
			maxIndex = i;
		}
	}

	char maxChar = 'a' + maxIndex;
	cout << maxChar << ' ' << count[maxIndex];
	//system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/sinat_42483341/article/details/87923902
今日推荐