PTA output GPLT

Export GPLT

(Article Directory)

Topic restatement

7-7 Output GPLT (20 points)
Given a character string composed of only English letters with a length not exceeding 10,000. Please readjust the order of the characters and output them in the order of GPLTGPLT... and ignore other characters. Of course, the number of the four characters (not case sensitive) is not necessarily the same. If a certain character has been output, the remaining characters will still be printed in the order of GPLT until all characters have been output.
Input format:

Enter a non-empty string of English letters that is not more than 10,000 in length on one line.

Output format:

Output the sorted string in one line according to the requirements of the title. The title guarantees that the output is not empty.

Input sample:

pcTclnGloRgLrtLhgljkLhGFauPewSKgt

Sample output:

GPLTGPLTGLTGLGLL

Problem analysis and solution ideas

1. First of all, first construct a string to accept all input characters, the length does not exceed 10000, the length of the string is sufficient.
2. Count the number of characters we need (both upper and lower case!).
3. GPLT outputs the character strings in the order given in the title. The n=Gn+Ln+Pn+Tn in the program is to ensure that all the remaining characters are printed in order.
The idea is very simple. The blogger has just started to write the solution of the problem, and many places have not considered thoroughly. I hope you can correct me!

code

#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    
    
    int Gn=0,Pn=0,Ln=0,Tn=0,n;
    string s;
    cin>>s;
    for(int i=0;i<s.length();i++)
    {
    
    if(s[i]=='G'||s[i]=='g') Gn++;
     else if (s[i]=='P'||s[i]=='p') Pn++;
     else if (s[i]=='L'||s[i]=='l') Ln++;
     else if (s[i]=='T'||s[i]=='t') Tn++;
     }
    n=Gn+Ln+Pn+Tn;
    for(int j=0;j<n;j++)
    {
    
    
        if(Gn>0){
    
    cout<<"G";Gn--;}
        if(Pn>0){
    
    cout<<"P";Pn--;}
        if(Ln>0){
    
    cout<<"L";Ln--;}
        if(Tn>0){
    
    cout<<"T";Tn--;}
    }
}

Guess you like

Origin blog.csdn.net/weixin_44108271/article/details/109412985