博客
关于我
团体程序设计天梯赛-练习集 L1-023 输出GPLT (20分)
阅读量:340 次
发布时间:2019-03-04

本文共 1292 字,大约阅读时间需要 4 分钟。

为了重新排列字符串并按GPLT顺序输出,我们可以使用四个栈分别存储G、P、L、T四个字符。这样在处理每个字符时,可以依次按照GPTL顺序输出字符,确保满足题目的要求。

首先,我们初始化四个栈,每个栈分别用于存储对应的字符。然后遍历输入字符串,遇到每个字符时,将其转换为大写,并将其压入对应的栈中。最后,依次检查每个栈是否为空,非空则取出栈顶字符输出,直到所有栈都为空。

这种方法能够处理各种字符数量不一致的情况,确保输出顺序正确。代码实现时,可以使用C++的stack来实现每个栈,并在主循环中依次输出字符。

以下是实现代码:

#include 
#include
#include
#include
using namespace std;int main() { stack
s1, s2, s3, s4; string str; cin >> str; for (char c : str) { char upper_c = toupper(c); switch (upper_c) { case 'G': s1.push(upper_c); break; case 'P': s2.push(upper_c); break; case 'L': s3.push(upper_c); break; case 'T': s4.push(upper_c); break; } } while (!s1.empty() || !s2.empty() || !s3.empty() || !s4.empty()) { if (!s1.empty()) { cout << s1.top(); s1.pop(); } if (!s2.empty()) { cout << s2.top(); s2.pop(); } if (!s3.empty()) { cout << s3.top(); s3.pop(); } if (!s4.empty()) { cout << s4.top(); s4.pop(); } } return 0;}

这个代码首先读取输入字符串,遍历每个字符并按顺序存储到四个栈中。然后,依次检查每个栈是否为空,非空则输出栈顶字符并弹出,直到所有栈为空。这样,输出的字符串将按照GPLT循环的顺序排列,满足题目的要求。

转载地址:http://tbfe.baihongyu.com/

你可能感兴趣的文章
npm报错File to import not found or unreadable: @/assets/styles/global.scss.
查看>>
npm报错unable to access ‘https://github.com/sohee-lee7/Squire.git/‘
查看>>
npm淘宝镜像过期npm ERR! request to https://registry.npm.taobao.org/vuex failed, reason: certificate has ex
查看>>
npm版本过高问题
查看>>
npm的“--force“和“--legacy-peer-deps“参数
查看>>
npm的安装和更新---npm工作笔记002
查看>>
npm的常用配置项---npm工作笔记004
查看>>
npm的问题:config global `--global`, `--local` are deprecated. Use `--location=global` instead 的解决办法
查看>>
npm编译报错You may need an additional loader to handle the result of these loaders
查看>>
npm设置淘宝镜像、升级等
查看>>
npm设置源地址,npm官方地址
查看>>
npm设置镜像如淘宝:http://npm.taobao.org/
查看>>
npm配置安装最新淘宝镜像,旧镜像会errror
查看>>
NPM酷库052:sax,按流解析XML
查看>>
npm错误 gyp错误 vs版本不对 msvs_version不兼容
查看>>
npm错误Error: Cannot find module ‘postcss-loader‘
查看>>
npm,yarn,cnpm 的区别
查看>>
NPOI
查看>>
NPOI之Excel——合并单元格、设置样式、输入公式
查看>>
NPOI初级教程
查看>>