字母统计

#include <iostream>
#include <vector>
using namespace std;
int main() {
	string s;
	cin >> s;
	vector<int> v(52, 0);
	for (int i = 0; i < s.length(); i++) {
		if (s[i] >= 'A' && s[i] <= 'Z') {
			v[s[i] - 65]++;
		}
		else if (s[i] >= 'a' && s[i] <= 'z') {
			v[s[i] - 97 + 26]++;
		}
	}

	int max = 0;
	char result = 123;
	for (int i = 0; i < 52; i++) {
		char temp;
		if (i >= 26) {
			temp = i - 26 + 97;
		}
		else {
			temp = i + 65;
		}
		if (v[i] > max) {
			max = v[i];
			result = temp;
		}
		else if (v[i] == max && temp < result) {
			result = temp;
		}

	}
	cout << result << endl;
	return 0;
}

  

原文地址:https://www.cnblogs.com/zhanghua-322/p/11302407.html