给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按GPLTGPLT…这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT的顺序打印,直到所有字符都被输出。
输入格式:
输入在一行中给出一个长度不超过10000的、仅由英文字母构成的非空字符串。
输出格式:
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
输入样例:
pcTclnGloRgLrtLhgljkLhGFauPewSKgt
输出样例:
GPLTGPLTGLTGLGLL
**思路:我做的时候出现了运行超时的错误,用while循环输出会比较慢,用for循环快一些,for的循环次数是对应G、P、L、T中的最大个数。代码比较简单,稍微仔细看一下就懂。
(知识点:这里用了c++中的max_element(数组起始地址,数组最后位置)函数,返回的是一个地址,要在前面加上’*’)**
#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
char s_G[1000];
char s_P[1000];
char s_L[1000];
char s_T[1000];
int count[4] = {0};
char c;
while(c !='\n')
{
scanf("%c",&c);
switch(c)
{
case 'g':
case 'G':count[0]++;break;
case 'p':
case 'P':count[1]++;break;
case 'l':
case 'L':count[2]++;break;
case 't':
case 'T':count[3]++;break;
default: break;
}
}
int max;
max = *max_element(count,count+4);
// printf("%d\n",max);
for(int i=0;i<max;i++)
{
if(count[0])
{
printf("G");count[0]--;
}
if(count[1])
{
printf("P");count[1]--;
}
if(count[2])
{
printf("L");count[2]--;
}
if(count[3])
{
printf("T");count[3]--;
}
}
return 0;
}