69. 完美度

Andrew-Malcom
#include<bits/stdc++.h>
using namespace std;
string lower(string s){
    for(int i=0;i<s.length();i++){
        if(s[i]>='A'&&s[i]<='Z'){
            s[i]+=32;
        }
    }
    return s;
}
bool cmp(int a,int b){
    return a>b;
}
int main()
{
    ios::sync_with_stdio(false);
    string s;
    cin>>s;cin.get();
    s=lower(s);
    int P = 26;long long int sum = 0;
    int* arry = new int[26];
    for(int i=0;i<26;i++){
        arry[i]=0;
    }
    //let us solve it !!!!!!
    for(int i=0;i<s.length();++i){
        arry[s[i]-'a']++;
    }
    sort(arry,arry+26,cmp);
    for(int i=0;i<26;i++){
        if(arry[i]==0) break;
        sum+=((P--)*arry[i]);
    }
    cout<<sum;
}
Commando
#include <bits/stdc++.h>

using namespace std;

using pairci = pair<char, int>;
using ull = unsigned long long;

struct comp {
    bool operator()(const pairci& lhs, const pairci& rhs) {
        return lhs.second < rhs.second;
    }
};

int main() {
    string str;
    cin >> str;

    unordered_map<char, int> hashmap;
    for (auto const& ch : str) ++hashmap[tolower(ch)];

    priority_queue<pairci, vector<pairci>, comp> queue(hashmap.begin(), hashmap.end());

    ull res = 0, val = 26;
    while (!queue.empty()) {
        auto const& p = queue.top();
        res += p.second * val--;
        queue.pop();
    }

    cout << res << endl;
}
computerLearner

一个很笨的原始方法, 供大家参考!

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int table[26];
bool cmp(int a, int b){
    return a > b;
}
int main(){
    string str;
    cin >> str;
    for(int i = 0; i < str.length(); i++){
        if(str[i] >= 65 && str[i] <= 90){
            //大写
            table[str[i] - 'A']++;
        }
        if(str[i] >= 'a' && str[i] <= 'z'){
            table[str[i] - 'a']++;
        }
    }
    sort(table, table+26, cmp);
    int res = 0;
    for(int i = 0; i < 26; i++){
        res = res + table[i]*(26-i);
    }
    cout << res;
}
你当前正在回复 博客/题目
存在问题!