题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062
Text Reverse
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16779 Accepted Submission(s): 6392
Problem Description
Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them.
Input
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.
Each test case contains a single line with several words. There will be at most 1000 characters in a line.
Output
For each test case, you should output the text which is processed.
Sample Input
3 olleh !dlrow m'I morf .udh I ekil .mca
Sample Output
hello world! I'm from hdu. I like acm.
注意:空格要按原样输出,有几个空格输出几个,还有一个c++里面代替getchar()的cin.get(),挺好用的
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sstream>
using namespace std;
int main()
{
int n;
cin>>n;
cin.get();
string s,m;
while(n--)
{
getline(cin,m);
istringstream str(m);
//输出样例前面的空格
while(str.peek()==' ')
cout<<(char)str.get();
while(str>>s)
{
for(int i = s.size()-1;i>=0;i--)
{
cout<<s[i];
}
//输出样例中间的空格
while(str.peek()==' ')
cout<<(char)str.get();
}
cout<<endl;
}
return 0;
}