PAT 乙级 -- 1009 -- 说反话
2024-08-29 10:28:01
题目简述
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
输出格式:每个测试用例的输出占一行,输出倒序后的句子。
输入样例:
Hello World Here I Come
输出样例:
Come I Here World Hello
思路
本题可自己实现split函数,C++使用STL类库较为方便。
C++代码样例
#include <iostream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
void split(vector<string>& str, const string& temp_str, const string& flag)
{
string::size_type pos_start, pos_end;
pos_start = 0;
pos_end = temp_str.find(flag);
while(string::npos != pos_end)
{
str.push_back(temp_str.substr(pos_start,pos_end-pos_start));
pos_start = pos_end + flag.size();
pos_end = temp_str.find(flag, pos_start);
}
if(pos_start != temp_str.length())
{
str.push_back(temp_str.substr(pos_start));
}
return;
}
int main(void)
{
vector<string> str;
string temp_str = "";
int len = 0;
char input_str[85] = "\0";
gets(input_str);
temp_str = input_str;
split(str,temp_str," ");
for(int i=str.size()-1; i>=0; i--){
printf("%s",(char*)str[i].data());
if(i != 0)
{
printf(" ");
}
}
return 0;
}
最新文章
- 微软专家推荐11个Chrome 插件
- nyoj 488 素数环
- 响应式设计的5个CSS实用技巧
- Android中的应用——谷歌官方Json分析工具Gson使用
- java打印正金字塔,倒金字塔和“水影”金字塔
- Java comparable 和 comparator
- Docker常见仓库Node.js
- Java8 新特性 | 如何风骚走位防止空指针异常
- js里面的判断最好做到完全控制
- mysql 删除表 外键出错
- TCP的状态转移
- 《基于Cortex-M4的ucOS-III的应用》课程设计 结题报告
- Jquery实现光棒效果
- HtmlRowCreated关于e.Row.Cells[0]的获取和设置
- lazy-mock ,一个生成后端模拟数据的懒人工具
- 【python3】将视频转换为代码视频
- WPF ListBoxItem DataTempldate command 执行问题
- 使用nginx反向代理处理前后端跨域访问
- 使用defined和require引入js
- 关于window.onload和body onload冲突的解决办法