PAT (Basic Level) Practice 1042 字符统计 (20 分)

题目:1042 字符统计 (20 分)

来源:PAT (Basic Level) Practice

传送门 1042 字符统计

题面

PAT (Basic Level) Practice 1042 字符统计 (20 分)

思路:字符串输入,for循环比较一遍即可。

Code

点击查看代码
#include<bits/stdc++.h> 
using namespace std;
int ans[300], Max = -1;
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	string s; char str;
	getline(cin, s);
	for (int i = 0; i < s.size(); i++) {

		s[i] = tolower(s[i]);
		ans[s[i] - '0']++;
		if (s[i] >= 'a' && s[i] <= 'z') {
			if (ans[s[i] - '0'] > Max || (ans[s[i] - '0'] == Max && s[i] < str)) {
				Max = ans[s[i] - '0'];
				str = s[i];
			}
		}
	}
	cout << str << " " << Max << "\n";
	return 0;
}

}
上一篇:remote: HTTP Basic:Access denied fatal:Authentication failed for


下一篇:java设计模式之组合模式