您的位置:首页 > 理论基础 > 计算机网络

Text Reverse&&http://acm.hdu.edu.cn/showproblem.php?pid=1062

2012-07-21 11:24 597 查看
[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.
这一题真他妈的恶心,本来是可以用sstream库函数的但是由于输入的字符串可能有多个空格也的输出,却不能用郁闷。。AC代码:
#include<iostream>
#include<cstring>
#include<sstream>
using namespace std;
int main(){
int t;
char a[1005],b[1005];
scanf("%d%*c",&t);
while(t--){
gets(a);
int i=0;
while(a[i]){
int k=0;
if(a[i]==' '){
printf(" ");
i++;
}
while(a[i]!=' ' && a[i])
b[k++]=a[i++];
b[k]='\0';
for(int j=strlen(b)-1;j>=0;j--)
printf("%c",b[j]);

}
puts("");
}
return 0;
}

第一次代码:
#include<iostream>
#include<set>
#include<cmath>
#include<string.h>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
char ch=getchar();
for(int k=1;k<=T;++k)
{
string s,str;
getline(cin,s);
istringstream stream(s);
int tot=0;
while(stream>>str)
{
if(tot++) str+="  ";
reverse(str.begin(),str.end());
cout<<str;
}
cout<<endl;
}return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息