給定一句英語,要求你編寫程序,將句中全部單詞的順序顛倒輸出。ios
測試輸入包含一個測試用例,在一行內給出總長度不超過 80 的字符串。字符串由若干單詞和若干空格組成,其中單詞是由英文字母(大小寫有區分)組成的字符串,單詞之間用 1 個空格分開,輸入保證句子末尾沒有多餘的空格。測試
每一個測試用例的輸出佔一行,輸出倒序後的句子。flex
Hello World Here I Come
Come I Here World Hello
利用棧的特性(後進先出),每輸入一個單詞就壓棧,最後依次彈出單詞spa
(棧頂)Hello - World - Here - I - Come(棧底)code
#include <iostream> #include <stack> #include <string> using namespace std; int main() { string str; stack<string> words; while (cin >> str) { words.push(str); } cout << words.top(); words.pop(); while (!words.empty()) { cout << ' ' << words.top(); words.pop(); } return 0; }