当前位置: 首页 > article >正文

HDU 1062:字符串反转

【题目来源】
http://acm.hdu.edu.cn/showproblem.php?pid=1062

【题目描述】
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.

【输入格式】
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.

【输出格式】
For each test case, you should output the text which is processed.

【输入样例】
3
olleh !dlrow
m'I morf .udh
I ekil .mca

【输出样例】
hello world!
I'm from hdu.
I like acm.

【算法代码】

#include <iostream>
#include <stack>
using namespace std;

int main() {
    char ch;
    int n;
    cin>>n;
    getchar(); //吸收cin>>n输入之后的换行符 
    while(n--) {
        stack<char> st;
        while(true) {
            ch=getchar();
            if(ch==' ' || ch=='\n') {
                while(!st.empty()) {
                    cout<<st.top();
                    st.pop();
                }
                if(ch==' ') cout<<' ';
                else break;
            } else st.push(ch);
        }
        cout<<endl;
    }
    return 0;
}


/*
in:
3
olleh !dlrow
m'I morf .udh
I ekil .mca

out:
hello world!
I'm from hdu.
I like acm.
*/ 



【参考文献】
https://blog.csdn.net/diaobin0154/article/details/101660651




 



 


http://www.kler.cn/news/108144.html

相关文章:

  • 面向对象设计模式——命令模式
  • 17、简单记录一下两个流媒体工具和推流测试
  • springboot配置https
  • 用于读取验证码的 OCR 模型
  • 设计模式:桥接模式(C#、JAVA、JavaScript、C++、Python、Go、PHP)
  • vue中父组件给子组件传递了参数后,什么时候确保子组件中收到的参数更新了
  • 创新领航 | 竹云参编《基于区块链的数据资产评估实施指南》正式发布!
  • 【Python机器学习】零基础掌握MissingIndicator缺失值填充
  • Node.js的基本概念node -v 和npm -v 这两个命令的作用
  • 华为数通方向HCIP-DataCom H12-831题库(多选题:101-120)
  • 2016年下半年上午易错题(软件设计师考试)
  • [100天算法】-连通网络的操作次数(day 46)
  • LVS-DR模式+keepalived+nginx+tomcat实现动静分离、负载均衡、高可用实验
  • 方舟生存进化ARK个人服务器搭建教程保姆级
  • 聊一下Word2vec-训练优化篇
  • 【Python爬虫三天从0到1】Day1:爬虫核心
  • 计算机视觉-光源的目的和作用
  • autoware.ai中检测模块lidar_detector caffe
  • vscode markdown 使用技巧 -- 如何快速打出一个Tab 或多个空格
  • Web3 治理实践探讨:如何寻找多元化发展路径?
  • 【python练习】在棋盘上收集奖品,跟着书本理思路
  • 2-Java进阶知识总结-6-多线程
  • shell脚本的编写(输入、输出、变量、数组等的使用规范及实例)
  • 【鸿蒙软件开发】ArkTS基础组件之TextClock(时间显示文本)、TextPicker(滑动选择文本)
  • 手写RPC框架
  • 网络安全(黑客)—小白自学
  • JavaScript的字符串介绍
  • Python 编写 Flink 应用程序经验记录(Flink1.17.1)
  • LuatOS-SOC接口文档(air780E)--max30102 - 心率模块
  • Geeker-Admin中ProTable表格分页之自定义接口页码、尺寸参数