简单来说就是统计G, P, L, T 出现的次数
然后按序输出
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 10000 + 10;
string s;
int q[N];
int main()
{
cin>>s;
int G = 0, P = 0, L = 0, T = 0;
for(int i = 0; i < s.size(); i++)
{
if('a' <= s[i] && s[i] <= 'z')
{
s[i] = (s[i] - ('a' - 'A'));
}
if(s[i] == 'G') G++;
if(s[i] == 'P') P++;
if(s[i] == 'L') L++;
if(s[i] == 'T') T++;
}
// cout<<G<<" "<<P<<" "<<L<<" "<<T;
while(G != 0 || P != 0 || L != 0 || T != 0)
{
if(G)
{
cout<<"G";
G --;
}
if(P)
{
cout<<"P";
P --;
}
if(L)
{
cout<<"L";
L --;
}
if(T)
{
cout<<"T";
T --;
}
}
return 0;
}
扩展
字符串大小写转换:
‘a’ - ‘A’ = 32;
if('a' <= s[i] && s[i] <= 'z')
{
s[i] = (s[i] - ('a' - 'A'));
}