L1-023 输出GPLT (20分)
给定一个长度不超过10000的、仅由英文字母构成的字符串。请将字符重新调整顺序,按GPLTGPLT....
这样的顺序输出,并忽略其它字符。当然,四种字符(不区分大小写)的个数不一定是一样多的,若某种字符已经输出完,则余下的字符仍按GPLT
的顺序打印,直到所有字符都被输出。
输入格式:
输入在一行中给出一个长度不超过10000的、仅由英文字母构成的非空字符串。
输出格式:
在一行中按题目要求输出排序后的字符串。题目保证输出非空。
输入样例:
pcTclnGloRgLrtLhgljkLhGFauPewSKgt
输出样例:
GPLTGPLTGLTGLGLL
我也不知道这么写有没有问题,但是确实简单的解决了问题。
那就直接统计个数,然后按照顺序一个一个的排队输出,没有了的当然也就停止不在输出了。
//#include<bits/stdc++.h> #include <iostream> #include <cstring> #include <string> #include <iomanip> // 格式化输入输出 #include <cmath> #include <cstdlib> #include <vector> using namespace std; void Function_023(int G,int P,int T,int L) { char a[4] = {'G','P','T','L'}; while( G + P + T + L > 0 ) { if(G != 0) { cout<<a[0]; G--; } if(P != 0) { cout<<a[1]; P--; } if(L != 0) { cout<<a[3]; L--; } if(T != 0) { cout<<a[2]; T--; } } } int main() { string str; cin>>str; int G,P,T,L,i = 0; G = P = T = L = 0; while(i < str.length()) { if(str[i] == 'g' || str[i] == 'G') G++; if(str[i] == 'p' || str[i] == 'P') P++; if(str[i] == 't' || str[i] == 'T') T++; if(str[i] == 'l' || str[i] == 'L') L++; i++; } Function_023(G,P,T,L); return 0; }