PAT 团体程序设计天梯赛-练习集 L1-023. 输出GPLT

给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按“GPLTGPLT....”这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT的顺序打印,直到所有字符都被输出。

输入格式:

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

输出格式:

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

输入样例:
pcTclnGloRgLrtLhgljkLhGFauPewSKgt
输出样例:
GPLTGPLTGLTGLGLL
 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<cstring>
 4 using namespace std;
 5 char GPLT[]={'G','P','L','T'};
 6 int main()
 7 {
 8     char s[10005];
 9     int gplt[4];
10     for(int i=0;i<4;i++)
11         gplt[i]=0;
12     cin>>s;
13     int len=strlen(s);
14     for(int i=0;i<len;i++)
15     {
16         if(s[i]=='G'||s[i]=='g')
17             gplt[0]++;
18         else if(s[i]=='P'||s[i]=='p')
19             gplt[1]++;
20         else if(s[i]=='L'||s[i]=='l')
21             gplt[2]++;
22         else if(s[i]=='T'||s[i]=='t')
23             gplt[3]++;
24     }
25     int sum=gplt[0]+gplt[1]+gplt[2]+gplt[3];
26     while(sum--)
27     {
28         for(int i=0;i<4;i++)
29             if(gplt[i]!=0)
30             {
31                 printf("%c",GPLT[i]);
32                 gplt[i]--;
33             }
34     }
35     printf("
");
36     return 0;
37 }

原文地址:https://www.cnblogs.com/Annetree/p/5669631.html