您的位置:首页 > 其它

HDU1062 Text Reverse

2016-04-03 13:01 423 查看


Text Reverse

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 25361 Accepted Submission(s): 9817



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.

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.

Hint
Remember to use getchar() to read '\n' after the interger T, then you may use gets() to read a line and process it.


这题和HDU 1321其实是一样的:http://acm.hdu.edu.cn/showproblem.php?pid=1321
可以看我的博客http://blog.csdn.net/xu_fish/article/details/51049241写的:

直接给出AC代码:
#include<iostream>
#include<cstring>
#include<string>
using namespace std;
void REVERSE(string &s1)
{
int len;
char ss[1005];
len = s1.length();
int j = len - 1;
for (int i = 0; i < len; i++,j--)
{
ss[j] = s1[i];
}
s1.clear();
for (int i = 0; i < len; i++, j--)
{
s1 += ss[i];
}
}
int main()
{
string str, s1;
char c;
int n;
cin >> n;
getchar();
while (n--)
{
for (int i = 0;;i++)
{
scanf("%c", &c);
if (c==' '&&c!='\n')
{
REVERSE(s1);
s1 += c;
str += s1;
s1.clear();
}
else if (c != '\n')
{
s1 += c;
}
else if (c == '\n')
{
REVERSE(s1);
str += s1;
s1.clear();
break;
}
}
cout << str << endl;
str.clear();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: