您的位置:首页 > 其它

HDU1062:Text Reverse

2016-02-21 16:26 281 查看
[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
For each test case, you should output the text which is processed.

[align=left]Sample Input[/align]

3
olleh !dlrow
m'I morf .udh
I ekil .mca

[align=left]Sample Output[/align]

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.

#include <iostream>
#include <stdio.h>
#include <string.h>

using namespace std;

int main()
{
int n,i,k1,k2;
char data[9999];
cin>>n;
getchar();
while(n--)
{
k1=0;k2=0;
gets(data);
for(i=0;i<=strlen(data);i++)
{
if(data[i]==' '||data[i]=='\0')//找空格,为了找单词
{
k2=i-1;//是空格前面那个词的下标
for(k2;k2>=k1;k2--)//从K2一直打到k1这个词
cout<<data[k2];
k1=i+1;//k1是下一个单词的开头的下标
if(data[i]==' ')
cout<<" ";
if(data[i]=='\0')
cout<<endl;
}
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: