给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过500 000的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用若干个空格分开。
输出格式:每个测试用例的输出占一行,输出倒序后的句子,并且保证单词间只有1个空格。
输入样例:
Hello World Here I Come
输出样例:
Come I Here World Hello
关键在于50000个字符,不能用字符数组
#include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<string> s; string sp; bool flag=false; while(cin>>sp) s.push(sp); while(!s.empty()) { if(flag) cout<<" "; else flag=true; cout<<s.top(); s.pop(); } return 0; }
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <iostream> 4 #include <string.h> 5 #include <string> 6 #include <math.h> 7 8 9 using namespace::std; 10 11 int main(){ 12 13 14 char s[500001]; 15 16 gets(s); 17 int start=0; 18 while(s[start] == ' ') 19 ++start; 20 /* 当输入为空格时,结束程序 */ 21 if(s[start] == '