首页 > 其他 > 详细

PAT B1043 输出PATest

时间:2021-05-19 10:27:37      阅读:18      评论:0      收藏:0      [点我收藏+]

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

输入格式:

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

输出格式:

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

输入样例:

redlesPayBestPATTopTeePHPereatitAPPT

输出样例:

PATestPATestPTetPTePePee

代码如下:

 

#include<cstdio>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
string str;
int hashtable[7]={0};
int main() {
    getline(cin, str);
    for (int i = 0; i < str.length(); i++) {
        if (str[i] == P) {
            hashtable[0]++;
        }
        else if (str[i] == A) {
            hashtable[1]++;
        }
        else if (str[i] == T) {
            hashtable[2]++;
        }
        else if (str[i] == e) {
            hashtable[3]++;
        }
        else if (str[i] == s) {
            hashtable[4]++;
        }
        else if (str[i] == t) {
            hashtable[5]++;
        }
    }
    int m=0;
    for (int i = 0; i < 6; i++) {
        if(hashtable[i]>m){
            m=hashtable[i];//循环次数为PATest中最多的字符数
        };
    }
        for (int j = 0; j <m; j++) {
            for (int i = 0; i < 6; i++) {
                if (hashtable[i] != 0) {
                    if (i == 0) {
                        printf("P");
                        hashtable[i]--;
                    }
                    else if (i == 1) {
                        printf("A");
                        hashtable[i]--;
                    }
                    else if (i == 2) {
                        printf("T");
                        hashtable[i]--;
                    }
                    else if (i == 3) {
                        printf("e");
                        hashtable[i]--;
                    }
                    else if (i == 4) {
                        printf("s");
                        hashtable[i]--;
                    }
                    else if (i == 5) {
                        printf("t");
                        hashtable[i]--;
                    }
                }
            }
        }
    printf("\n");
    return 0;
}

 

PAT B1043 输出PATest

原文:https://www.cnblogs.com/migang/p/14781812.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!