英文句子的逆序

将一个英文语句以单词为单位逆序排放。例如“I am a boy”,逆序排放后为“boy a am I”,所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含其他字符。

(说声悄悄话,可以很自然的联想到向量这种增强版的数组这种数据结构来嘞!!!)

Less words,and more codes:

First: use vector<string> 

#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	vector<string> vs;// 这种宏观的思维调运待我熟稔之!
	while(cin>>s) vs.push_back(s);
	for(int i=vs.size()-1;i>0;i--) 
		cout<<vs[i]<<" ";
	cout<<vs[0]<<endl;
	return 0;
}

Second: use stringstream:    //此处待追寻之:https://blog.csdn.net/sunshineacm/article/details/78068987

 1 #include<bits/stdc++>h>
 2 using namespace std;
 3 int main()
 4 {
 5     string s;
 6     getline(s);
 7     stringstream ss(s);  
 8     string res=" ",tmp;
 9     while(ss>>tmp)
10     {
11         if(res=" ") res=tmp;
12         else res=tmp+" "+res;
13     }
14 }

Third:用reverse,先整体反转再局部反转。  //我还没没有理解,等待着稍后的思考理解,嘻嘻!


#include<bits/stdc++.h>
using namespace std;
int main(){
string s;
while(getline(cin,s)){
reverse(s.begin(),s.end());
string::iterator left=s.begin();
for(string::iterator itr=s.begin();itr!=s.end()+1;++itr){
if(itr==s.end()||*itr==' '){
reverse(left,itr);
left=itr+1;
}
}
cout<<s<<endl;
}
}//待思之!


Fourth: 

Python way:

1 print(" ".join(input().split()[::-1]))

写到这儿,刷了一些题目,下面要去系统性地浏览一下ACM的书籍,加油鸭,龙龙!

  

原文地址:https://www.cnblogs.com/dragondragon/p/11196086.html