PAT (Basic Level) Practice (中文)B1043 输出PATest (20 分)(C++)(散列)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37454852/article/details/86482776

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

输入格式:

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

输出格式:

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

输入样例:

redlesPayBestPATTopTeePHPereatitAPPT
输出样例:

PATestPATestPTetPTePePee


using namespace std;
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>

string str;
int Cnt[10] = {0};//为正数则表示存在多少个,小于等于零表示没有了
int main()
{
    getline(cin, str);
    int len = str.length();
    for(int i=0; i<len; i++)
    {
        switch(str[i])//计算字符串中PATest各字符的个数
        {
            case 'P': Cnt[0]++; break;
            case 'A': Cnt[1]++; break;
            case 'T': Cnt[2]++; break;
            case 'e': Cnt[3]++; break;
            case 's': Cnt[4]++; break;
            case 't': Cnt[5]++; break;
        }
    }
    while(Cnt[0]>0 || Cnt[1]>0 || Cnt[2]>0 || Cnt[3]>0 || Cnt[4]>0 || Cnt[5]>0)//若其中仍有某字符存在则继续打印
    {
        if(Cnt[0]>0) printf("P");
        if(Cnt[1]>0) printf("A");
        if(Cnt[2]>0) printf("T");
        if(Cnt[3]>0) printf("e");
        if(Cnt[4]>0) printf("s");
        if(Cnt[5]>0) printf("t");
        for(int i=0; i<6; i++) Cnt[i]--;//全部减一
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37454852/article/details/86482776
今日推荐