P1598 垂直柱状图

题目传送门

#include <bits/stdc++.h>

using namespace std;
int n = 4;
string s;
unordered_map<char, int> _map;

int main() {
    //读入并记录
    while (n--) {
        getline(cin, s);
        for (int i = 0; i < s.size(); i++) if (s[i] >= 'A' && s[i] <= 'Z') _map[s[i]]++;
    }
    //1、找出最大值,这个最大值就是行数
    int Max = -1;
    for (auto it = _map.begin(); it != _map.end(); it++) Max = max(Max, it->second); //Hash表的遍历
    //2、从大到小
    for (int i = Max; i >= 1; i--) {
        //每一列噢.对应着字母A-Z
        for (int j = 0; j < 26; j++)
            if (_map['A' + j] >= i) cout << "* ";
            else cout << "  ";
        cout << endl;
    }
    //3、补一下大写字母
    for(int i=0;i<26;i++)cout<<char(i+'A')<<" ";
    return 0;
}

原文地址:https://www.cnblogs.com/littlehb/p/15571321.html