时间限制: 1.000 Sec 内存限制: 128 MB
题目描述
KiKi likes to write words in reverse way. Given a single line of text which is written by KiKi, you should reverse all the words and then output them.
输入
The input contains several test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
输出
For each test case, you should output the text which is processed.
样例输入 Copy
3 olleh !dlrow m'I morf .cpu I ekil .mca
样例输出 Copy
hello world! I'm from upc. I like acm.
句子前后都有空格的时候,坑就出现了。
所以我WA了两次QAQ
下面是AC代码。
#include <bits/stdc++.h>
using namespace std;
int main()
{
int t;
cin >> t;
getchar();
while (t--)
{
vector<string> words;
vector<string>::iterator it;
string temp = "";
char c;
while (1)
{
c = getchar();
if (c != ' ' && c != '\n')
{
temp += c;
}
else if ((c == ' ' || c == '\n') && temp != "")
{
words.push_back(temp);
temp = "";
if (c == '\n')
{
break;
}
}
}
for (it = words.begin(); it != words.end(); it++)
{
int L = (*it).size();
int i;
for (i = 0; i < L / 2; i++)
{
swap((*it)[i], (*it)[L - i - 1]);
}
}
for (auto &s : words)
{
cout << s << ' ';
}
cout << '\n';
}
return 0;
}