PAT 1043 输出PATest python

1043 输出PATest(20 分)

给定一个长度不超过 10​4 的、仅由英文字母构成的字符串。请将字符重新调整顺序,按 PATestPATest…. 这样的顺序输出,并忽略其它字符。当然,六种字符的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按 PATest 的顺序打印,直到所有字符都被输出。

输入格式:
输入在一行中给出一个长度不超过 10​4 的、仅由英文字母构成的非空字符串。

输出格式:
在一行中按题目要求输出排序后的字符串。题目保证输出非空。

输入样例:

redlesPayBestPATTopTeePHPereatitAPPT

输出样例:

PATestPATestPTetPTePePee

作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB

问题分析:
将所有PATest进行统计,然后依次输出

代码:

ss = input()
P = A = T = e = s = t = 0
for i in ss:
    if i == 'P':
        P += 1
    elif i == 'A':
        A += 1
    elif i == 'T':
        T += 1
    elif i == 'e':
        e += 1
    elif i == 's':
        s += 1
    elif i == 't':
        t += 1

while P or A or T or e or s or t:
    if P > 0:
        print('P', end='')
        P -= 1
    if A > 0:
        print('A', end='')
        A -= 1
    if T > 0:
        print('T', end='')
        T -= 1
    if e > 0:
        print('e', end='')
        e -= 1
    if s > 0:
        print('s', end='')
        s -= 1
    if t > 0:
        print('t', end='')
        t -= 1

猜你喜欢

转载自blog.csdn.net/qq_42800234/article/details/82380644