PAT 天梯赛 L1-023. 输出GPLT 【水】

本文提供了一段C++代码,用于解决PATest平台上的字符分类与排序问题。该程序读取一个字符串,并将其中的大写和小写的G、P、L、T字符分别计数后按顺序输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

https://www.patest.cn/contests/gplt/L1-023

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;

typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e5 + 5;
const int MOD = 1e9 + 7;

int main()
{
    string s;
    cin >> s;
    int len = s.size();
    int i;
    int a[4] = {0};
    for (i = 0; i < len; i++)
    {
        if (s[i] == 'G' || s[i] == 'g')
            a[0]++;
        else if (s[i] == 'P' || s[i] == 'p')
            a[1]++;
        else if (s[i] == 'L' || s[i] == 'l')
            a[2]++;
        else if (s[i] == 'T' || s[i] == 't')
            a[3]++;
    }
    while (a[0] || a[1] || a[2] || a[3])
    {
        if (a[0])
        {
            printf("G");
            a[0]--;
        }
        if (a[1])
        {
            printf("P");
            a[1]--;
        }
        if (a[2])
        {
            printf("L");
            a[2]--;
        }
        if (a[3])
        {
            printf("T");
            a[3]--;
        }
    }
    cout << endl;
}
### L1-023 GPLT Java 实现 为了实现L1-023问题的要求,即根据输入字符串中的字符"G", "P", "L", 和"T"的数量来构建并输出新的字符串,可以采用以下方法: #### 方法概述 通过读取输入字符串并将所有字母转换为大写形式以便于处理。随后统计所需四个特定字符(G, P, L, T)各自的数量。最后依据GPLT的固定模式依次减少相应计数器直至全部字符被消耗完毕。 下面是具体的Java代码实现: ```java import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String input = scanner.nextLine().toUpperCase(); int gCount = 0; int pCount = 0; int lCount = 0; int tCount = 0; // 统计各个字符的数量 for (char c : input.toCharArray()) { switch(c){ case 'G': ++gCount; break; case 'P': ++pCount; break; case 'L': ++lCount; break; case 'T': ++tCount; break; } } StringBuilder resultBuilder = new StringBuilder(); // 按照GPLT顺序循环添加字符到结果串中 while(gCount > 0 || pCount > 0 || lCount > 0 || tCount > 0){ if(gCount-- > 0) resultBuilder.append('G'); if(pCount-- > 0) resultBuilder.append('P'); if(lCount-- > 0) resultBuilder.append('L'); if(tCount-- > 0) resultBuilder.append('T'); } System.out.println(resultBuilder.toString()); } } ``` 这段代码实现了对输入字符串的操作,并严格按照GPLT序列重复排列这些指定字符,直到所有的目标字符都已经被加入到了最终的结果字符串里[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值