首页 > 学院 > 开发设计 > 正文

华为OJ:名字的漂亮度

2019-11-08 02:12:02
字体:
来源:转载
供稿:网友

题目描述 给出一个名字,该名字有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;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表