题目链接:https://www.luogu.com.cn/problem/P1598
19行就是解题关键
1 #include <bits/stdc++.h> 2 using namespace std; 3 int main() { 4 map<char, int> m; 5 char c; 6 while (cin >> c) { 7 if (c >= 'A' && c <= 'Z') { //只统计大写字母 8 m[c]++; 9 } 10 } 11 int maxx = -1; 12 for (map<char, int>::iterator it = m.begin(); it != m.end(); it++) { 13 if (it -> second > maxx) { 14 maxx = (it -> second); //取直方图最大高度 15 } 16 } 17 for (int i = 0; i < maxx; i++) { //行数就是直方图的最大高度 18 for (int j = 0; j < 26; j++) { //26列 19 if (i >= maxx - m[j + 'A']) { //确保底在下面 20 cout << "* "; 21 } else { 22 cout << " "; 23 } 24 } 25 cout << endl; 26 } 27 for (map<char, int>::iterator it = m.begin(); it != m.end(); it++) { 28 cout << (it -> first) << " "; 29 } 30 return 0; 31 }