PAT 7-8 說反話-加強版
7-8 說反話-加強版(20 分)
給定一句英語,要求你編寫程式,將句中所有單詞的順序顛倒輸出。
輸入格式:
測試輸入包含一個測試用例,在一行內給出總長度不超過500 000的字串。字串由若干單詞和若干空格組成,其中單詞是由英文字母(大小寫有區分)組成的字串,單詞之間用若干個空格分開。
輸出格式:
每個測試用例的輸出佔一行,輸出倒序後的句子,並且保證單詞間只有1個空格。
輸入樣例:
Hello World Here I Come
輸出樣例:
Come I Here World Hello
提示:第一次提交報格式錯誤, 修改後發現是最後一次輸出是多輸出了一個空格
#include <iostream>
#include <string>
#include <stack>
using namespace std;
int main(void)
{
string str;
stack<string> s;
while (cin >> str)
{
s.push(str);
}
while (!s.empty())
{
if (s.size() > 1)
{
cout << s.top() << " ";
s.pop();
}
else
{
cout << s.top();
s.pop();
}
}
cout << endl;
return 0;
}