題目描述 給出一個名字,該名字有26個字符串組成,定義這個字符串的“漂亮度”是其所有字母“漂亮度”的總和。 每個字母都有一個“漂亮度”,范圍在1到26之間。沒有任何兩個字母擁有相同的“漂亮度”。字母忽略大小寫。 給出多個名字,計算每個名字最大可能的“漂亮度”。
輸入描述: 整數N,后續N個名字
輸出描述: 每個名稱可能的最大漂亮程度
輸入例子: 2 zhangsan lisi
輸出例子: 192 101
解析:#include <iostream>#include <vector>#include <algorithm>#include <string>using namespace std;int main(){ int i,j=0,k,n; vector<string>vec; string str; int sumA; while(cin>>n) { vec.clear(); for(i=0;i<n;++i) { cin>>str; vec.push_back(str); str.clear(); } for(i=0;i<n;++i) { sumA = 0; int count_alpha[26]={0}; for(j=0;j<vec[i].length();++j) { if(vec[i].at(j)>='A'&&vec[i].at(j)<='Z') { vec[i].at(j)+=32; } count_alpha[vec[i].at(j)-'a']++; } sort(count_alpha,count_alpha+26); for(k=0;k<26;++k) { sumA+=(count_alpha[k]*(k+1)); } cout<<sumA<<endl; } } return 0;}//解法二#include <iostream>using namespace std;int main(){ int test; while (cin >> test) { while (test--) { string st; cin >> st; int i, a[26] = {0}, k = 26, res = 0; for (i = 0; i < st.length(); ++i) { if (st[i] >= 'a' && st[i] <= 'z') a[st[i] - 'a']++; else a[st[i] - 'A']++; } sort(a, a + 26); for (i = 25; i >= 0; --i) res += a[i] * k--; cout << res << endl; } } return 0;}新聞熱點
疑難解答