本文同步发于洛谷博客,您也可以在题解页面访问。

题目传送门

水题

看不懂stringstream,所以用了这种方法,【分析】可以直接用cin>>…读入。(cin遇到空格,回车,TAB停止读入

实现方法:

看个例子: I love you.会读入Iloveyou.

怎么实现?直接用

1
2
3
4
string s;
while(cin>>s){
/*语句段*/
}

它会一直读入数据,直到EOF(输入中止结束符)

百科

https://baike.baidu.com/item/EOF/1017800?fr=aladdin

模拟:(以I love you.为例:

首先读入I,遇到空格,执行语句段,然后读入love……最后读入you.遇到EOF,结束。

reverse(s.begin(),s.end());:reverse:定义在<algorithm>中,指将字符串、数组等反转,s.begins.end是两个广义指针,分别指向s的开头和末尾。

介绍一下getchar:

getchar原本的意思指读入一个字符,在这里可以把两个字符串之间的空格/回车”吃掉“,示例如下:

之后怎么办?输出就好了。

CODE::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<string> //string类型用
#include<cstdio> //geichar()用
#include<algorithm> //reverse()用
//并不向初学者推荐使用万能头,我们要明白每个头文件有什么用途。
using namespace std;
int main(){
string s;
while(cin>>s){
reverse(s.begin(),s.end());
cout<<s;
char ch=getchar();
cout<<ch;
}
return 0;
}

Update:

2021.5.29 重新编写对getchar的定义。