统计各大写字母频次,自最高频向下逐层打印星号。
OJ: noi_openjudge
题目 ID: ch0113-04
难度:普及-
标签:字符串模拟python
日期: 2026-07-30 23:01
题意
统计四行文本中 A 到 Z 的出现次数,输出从上到下的垂直直方图。
思路
先用 Counter 统计频次,最高频决定图的高度。对每一层 level,当某字母频次不少于该层就输出 *,否则输出空格;每行右侧无意义空格用 rstrip 删除。
代码
Python代码
python
from collections import Counter
counts = Counter("".join(input() for _ in range(4)))
height = max(counts[chr(code)] for code in range(ord("A"), ord("Z") + 1))
for level in range(height, 0, -1):
line = " ".join(
"*" if counts[chr(code)] >= level else " "
for code in range(ord("A"), ord("Z") + 1)
)
print(line.rstrip())
print(" ".join(chr(code) for code in range(ord("A"), ord("Z") + 1)))复杂度
设最高频为
总结
按高度从大到小打印,可以直接得到竖直图形,不必额外旋转矩阵。