解题思路: 记录所有字母出现次数
最多出现次数 - 最少出现次数 = 答案
未出现的字母不算数
注意事项:
参考代码:
#include<bits/stdc++.h> using namespace std; int a[26]; int main(){ string str; cin >> str; int len = str.length(); for(int i = 0; i < len; i ++) { //统计所有字母出现次数 a[str[i] - 'a'] ++; } sort(a, a + 26); //小前大后,排序 for(int i = 0; i < 26; i ++) { //遍历,找,已出现且次数最少 if(a[i] != 0) { cout << a[25] - a[i]; //减去最多,即答案 return 0; } } cout << 0; //执行到这里,表示没找到,输出个0 return 0; }
0.0分
3 人评分