当前位置:网站首页>PAT乙级-B1009 说反话(20)
PAT乙级-B1009 说反话(20)
2022-08-03 14:20:00 【nekoha_dexter】
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:
测试输入包含一个测试用例,在一行内给出总长度不超过 80 的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用 1 个空格分开,输入保证句子末尾没有多余的空格。
输出格式:
每个测试用例的输出占一行,输出倒序后的句子。
输入样例:
Hello World Here I Come
输出样例:
Come I Here World Hello
#include<iostream>
#include<string>
using namespace std;
//1、用string整行存入
//2、从后到前遍历string,遇到空格时开始处理数据
//2.1、用pt记录空格位置,其初始值指向队尾后,flag记录是否已输出过
//2.2、当flag ==1时,需要输出空格
//2.3、输出下标从空格+1~pt-1的子串,string.substr(position,length)
//2.4、输出后使pt指向空格处
//2.5、最后记得输出下标 0 ~ pt-1的子串
int main(){
string str;
getline(cin, str);
int pt = str.size(), flag = 0;
for(int i = pt - 1; i >= 0; --i)
if(str[i] == ' '){
if(flag) cout << " ";
cout <<str.substr(i+1,pt - i - 1);
flag = 1;
pt = i;
}
if(flag) cout << " ";
cout << str.substr(0,pt)<<endl;
return 0;
}
边栏推荐
- 第二讲 软件生命周期
- Huffman tree
- CVPR 2022 | Predicting Skeletons from Human Meshes, True Physiological Skeletons!
- 用1000行代码统计西安新房价格后,我有一个惊人的发现……
- Php程序员用那个编辑器比较好?
- 软件测试考证:ISTQB、软件评测师
- With 1000 lines of code statistics after the xi 'an housing prices, I have a startling discovery...
- STL简介
- 【问题】使用pip安装第三方库的时候遇到“timeout”的解决方法
- 面试官都震惊,你这“网络基础”可以啊
猜你喜欢
20220801使用安信可的ESP-01S模块实现WIFI的UART传输功能
With 1000 lines of code statistics after the xi 'an housing prices, I have a startling discovery...
系统学习Shell之正则表达式
Lecture 2 Software Life Cycle
162_Power Query is a custom function for quickly merging tables in a folder TableXlsxCsv_2.0
HCIP Day 16 Notes (SVI, Spanning Tree Protocol)
PMP每日一练 | 考试不迷路-8.3(包含敏捷+多选)
Mysql 生成排序序号
你把 vite打包 玩明白
如何在 UE4 中制作一扇自动开启的大门
随机推荐
哥斯拉加密WebShell过杀软
“芯片法案”通过后,美光承诺在美国扩产
Left index of all anagrams in leetcode/string (some permutation of s1 string is a substring of s2)
项目管理:PMP和IPMP哪个更值得考?两个证书的区别在于哪里?
Jupyter Notebook 交互式编程 & 低代码拖拽式编程 | 数据科学生态下的理想平台
背后的力量 | 提升医疗服务“速度“和“温度” 华云数据助力上海国际医学中心加速智慧医院建设
What are the visual database design software _ database visual programming
[A summary of the sorting and use of activation functions in deep learning]
【问题】torch和torchvision对应版本
你把 浏览器滚动事件 玩明白
PCL 点云按时间进行分段
输出1!+2!+3!+......+n!
atrace和systrace的基本使用方法
爬虫——代理搭建、爬取视频网站、爬取新闻、BeautifulSoup4介绍、bs4 遍历文档树、bs4搜索文档树、bs4使用选择器
QImageReader
【报错】import cv2 as cv ModuleNotFoundError: No module named ‘cv2
Role usage in Ansible
工作流自动化,低代码是解决关键
【框架】idea找不到xxx依赖项怎么办
驻冰岛使馆提醒旅冰中国公民务必加强安全防护