PTA basic 1043 输出PATest (20 分) c++语言实现(g++)

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

输入格式:

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

输出格式:

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

输入样例:

redlesPayBestPATTopTeePHPereatitAPPT
 

输出样例:

PATestPATestPTetPTePePee

 

ascii字典计数,按照要求顺序输出即可

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 using namespace std;
 5 int main(){
 6     string str;
 7     vector<int> alpha(129,0);
 8     cin >> str;
 9     int P{static_cast<int>('P')};
10     int A{static_cast<int>('A')};
11     int T{static_cast<int>('T')};
12     int e{static_cast<int>('e')};
13     int s{static_cast<int>('s')};
14     int t{static_cast<int>('t')};
15     
16     for(auto c:str){
17         alpha[static_cast<int>(c)]+=1;
18     }
19     int count=alpha[P]+alpha[A]+alpha[T]+alpha[t]+alpha[e]+alpha[s];
20     for(int i=0;i<count;i++){
21         if(alpha[P]-->0)printf("P");
22         if(alpha[A]-->0)printf("A");
23         if(alpha[T]-->0)printf("T");
24         if(alpha[e]-->0)printf("e");
25         if(alpha[s]-->0)printf("s");
26         if(alpha[t]-->0)printf("t");
27     }
28     return 0;
29 }

 

posted @ 2021-05-07 10:34  keiiha  阅读(70)  评论(0编辑  收藏  举报