洛谷 P1598 垂直柱状图
思路
用大小为26的数组存储每个字母出现的次数。
先用Max求出出现次数最多的字母的出现次数。
然后每次遍历,如果该字母出现次数>=Max,则输出'*';
否则输出' '。
最后一行原样输出就行。
代码
#include <bits/stdc++.h>
using namespace std;
int main() {
freopen("in.txt", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
int n = 4;
int arr[26] = {0};
while (n--) {
getline(cin, s);
for (char i : s) {
if (i >= 'A' && i <= 'Z') {
arr[i - 'A']++;
}
}
}
int Max = 0;
for (int i : arr) {
if (i > Max) {
Max = i;
}
}
while (Max > 0) {
for (int i = 0; i < 25; i++) {
if (arr[i] >= Max) {
cout << "* ";
} else {
cout << " ";
}
}
if (arr[25] >= Max) {
cout << "*" << endl;
} else {
cout << " " << endl;
}
Max--;
}
cout << "A B C D E F G H I J K L M N O P Q R S T U V W X Y Z";
return 0;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。