首页 > 试题广场 >

字母统计

[编程题]字母统计
  • 热度指数:17146 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入一行字符串,计算其中A-Z大写字母出现的次数

输入描述:
案例可能有多组,每个案例输入为一行字符串。


输出描述:
对每个案例按A-Z的顺序输出其中大写字母出现的次数。
示例1

输入

DFJEIWFNQLEF0395823048+_+JDLSFJDLSJFKK

输出

A:0
B:0
C:0
D:3
E:2
F:5
G:0
H:0
I:1
J:4
K:2
L:3
M:0
N:1
O:0
P:0
Q:1
R:0
S:2
T:0
U:0
V:0
W:1
X:0
Y:0
Z:0
while True:
    try:
        inp=list(input().strip())
        #print(inp)
        #inp=sorted(inp)
        for i in range(65,91):
            #print(chr(i))
            index=chr(i)
            if index in inp:
                num=inp.count(index)
                print(index+':'+str(num))
            else:
                print(index+':'+'0')

    except:
        break
编辑于 2019-07-31 08:54:43 回复(0)

简单两步,输入~输出  ~~~~(>_<)~~~~

while True:
    try:
        string = input()
        for i in range(65,91):
            print("%s:%d" % (chr(i),string.count(chr(i))))
    except Exception:
        break
编辑于 2018-10-12 09:34:52 回复(0)

python简单的解法,使用defaultdict来解,再适合不过了。

from collections import defaultdict

while True:
    try:
        a, dd = input(), defaultdict(int)
        for i in a:
            if i.isupper(): dd[i] += 1
        for i in range(65, 91):
            print(chr(i) + ":" + str(dd[chr(i)]))

    except:
        break
发表于 2017-10-06 16:35:13 回复(0)
from collections import Counter
from string import ascii_uppercase as a
try:
    while 1:
        L = Counter(filter(lambda x:x.isupper() is True, raw_input()))
        for i in a:
            print i + ':' + str(L[i]) if i in a else i + ':' + '0'
except:
    pass

发表于 2016-12-27 09:18:51 回复(0)