7-12 說反話-加強版(20 分)
給定一句英語,要求你編寫程序,將句中所有單詞的順序顛倒輸出。
輸入格式:
測試輸入包含一個測試用例,在一行內給出總長度不超過500 000的字符串。字符串由若干單詞和若干空格組成,其中單詞是由英文字母(大小寫有區分)組成的字符串,單詞之間用若干個空格分開。
輸出格式:
每個測試用例的輸出佔一行,輸出倒序後的句子,並且保證單詞間只有1個空格。
輸入樣例:
Hello World Here I Come
輸出樣例:
Come I Here World Hello
#include<bits/stdc++.h>
using namespace std;
int main()
{
string line;
getline(cin,line);
stack<char>cnt;
int i,j;
int first = 0;
for(i = line.length() - 1;i>=0;i--)
{
if(line[i] != ' '&&i != 0)
cnt.push(line[i]);
else {
if(first != 0 && !cnt.empty()) cout<< " ";
if(i == 0 && line[0] != ' ')cnt.push(line[0]);
int flag = 0;
while(!cnt.empty()){
cout << cnt.top();
cnt.pop();
flag = 1;
}
if(flag == 1)
first++;
}
}
cout<<endl;
return 0;
}