L1-023. 输出GPLT

题目链接:https://www.patest.cn/contests/gplt/L1-023

思路:读入字符串,如何遍历GPLT的个数,再按此顺序输出,输出完的跳过不处理,直到都输出完。

注意点:大小没关系,输出都为大写,所以可以用两个相邻的case语句来处理。

 1 #include<stdio.h>
 2 #include<string.h>
 3 int main(){
 4     char str[10000];
 5     scanf("%s",str);
 6     int i,n,ng,np,nl,nt;
 7     n=strlen(str);
 8     ng=0,np=0,nl=0,nt=0;
 9     for(i=0;i<n;i++)
10     {
11         switch(str[i]){
12             case 'g':
13             case 'G':
14                 ng++;
15                 break;
16             case 'p':
17             case 'P':
18                 np++;
19                 break;
20             case 'l':
21             case 'L':
22                 nl++;
23                 break;
24             case 't':
25             case 'T':
26                 nt++;
27                 break;    
28         }
29     }
30     while(ng||np||nl||nt)
31     {
32         if(ng){
33             printf("G");
34             ng--;
35         }
36         if(np){
37             printf("P");
38             np--;
39         }
40         if(nl){
41             printf("L");
42             nl--;
43         }
44         if(nt){
45             printf("T");
46             nt--;
47         }
48     }
49     printf("\n");
50     return 0;
51 }

 

posted @ 2018-01-19 14:57  爱你的笑  阅读(169)  评论(0编辑  收藏  举报