名字的漂亮度(HJ45)
一:解题思路
这道题目的关键的地方在于,让出现字母次数最多的字母,漂亮度最大,其次依次递减。
二:完整代码示例 (C++版和Java版)
C++代码:
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { int n = 0; while (cin >> n) { while (n--) { string str = ""; cin >> str; int k = 26; int a[26] = {0}; int res = 0; for (int i = 0; i < str.size(); i++) { if (str[i] >= 'a' && str[i] <= 'z') a[str[i] - 'a']++; else a[str[i] - 'A']++; } sort(a,a+26); for (int i = 25; i >= 0; i--) { if (a[i] != 0) res += a[i] * k--; } cout << res << endl; } } return 0; }

浙公网安备 33010602011771号