PAT (Basic Level) Practice (中文)1043 输出PATest (20 分)

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

输入格式:

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

输出格式:

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

输入样例:

redlesPayBestPATTopTeePHPereatitAPPT

输出样例:

PATestPATestPTetPTePePee
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <string>
 4 #include <cstring>
 5 using namespace std;
 6 int cnt[6];
 7 char a[6]={'P','A','T','e','s','t'};
 8 string s;
 9 int main()
10 {
11     while(cin>>s){
12         memset(cnt,0,sizeof(cnt));
13         int len=s.length();
14         int sum=0;
15         for(int i=0;i<len;i++){
16             for(int j=0;j<6;j++){
17                 if(s[i]==a[j]){
18                     cnt[j]++;
19                     sum++;
20                     break;
21                 }
22             }
23         }
24         int i=0;
25         while(sum){
26             if(cnt[i]){
27                 cout<<a[i];
28                 //cout<<a[i]<<" "<<cnt[i]<<endl;
29                 cnt[i]--;
30                 sum--;
31             }
32             i++;
33             i=i%6;
34         }
35         cout<<endl;
36     }
37     return 0;
38 }
原文地址:https://www.cnblogs.com/wydxry/p/11187977.html