题解 | #名字的漂亮度#

名字的漂亮度

https://www.nowcoder.com/practice/02cb8d3597cf416d9f6ae1b9ddc4fde3

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main() {
    int N;
    cin >> N;
    string s;
    vector<char> c;//用c存储s中的不同字符
    vector<int> num;//用num存储c中字符出现的次数
    while (cin >> s) {
        auto it = s.begin();
        c.push_back(*it);
        num.push_back(count(s.begin(), s.end(), *it));
        ++it;
        while (it != s.end()) {
            if (find(c.begin(), c.end(), *it) == c.end()) {//如果*it不在c中
                c.push_back(*it);
                num.push_back(count(s.begin(), s.end(), *it));
            }
            ++it;
        }
        int i, j;
        for (i = 0; i < num.size() - 1; ++i) {//冒泡排序
            for (j = num.size() - 1; j > i; --j) {
                if (num[j - 1] < num[j]) {
                    swap(num[j - 1], num[j]);
                    swap(c[j - 1], c[j]);//好像不需要这个操作
                }
            }
        }
        int name_beauty = 0;
        for (i = 0; i < c.size(); ++i) {
            name_beauty += num[i] * (26 - i);//出现最多的字符赋26,依次类推
        }
        cout << name_beauty << endl;
        c.clear();//记得把c和num清空
        num.clear();
    }
    return 0;
}

看懂题目后本题就很简单了:一个“名字”中至多有26种字母,对出现次数最多的字母赋予最大的漂亮度26,并依次类推即可。

全部评论

相关推荐

身边有人上海、深圳&nbsp;6、7k&nbsp;都去了,真就带薪上班了。
小浪_coder:深圳除了一些计算机,UI设计,金融类等一些可以月薪过万的工作之外, 认识很多朋友做运营,营销,文员的工作, 月薪基本都在4-6K左右,还有大把人在干
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-04 14:35
点赞 评论 收藏
分享
醉蟀:你不干有的是人干
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务