题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062

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.

解题思路:问题求解的是每个句子中每个单词的反转。

AC代码一之C代码:

 #include<bits/stdc++.h>
using namespace std;
char a[];
int main()
{
int T,len,x,y;
while(cin>>T){
getchar();//吃掉回车符
while(T--){
gets(a);
len=strlen(a);
for(int i=;i<len;++i){
x=i;//标记当前单词的起始坐标
while(a[i]!=' ' && a[i]!='\0')++i;//循环直到遇到空字符
y=i-;//标记单词尾坐标
for(int j=y;j>=x;--j)
printf("%c",a[j]);//反序输出
if(a[i]==' ')cout<<' ';//如果此时a[i]是空字符的话顺便输出
}
cout<<endl;//换行
}
}
return ;
}

AC代码二之C++代码:

 #include<bits/stdc++.h>
using namespace std;
int t;string str,tmp;size_t pos,pre;
int main(){
while(cin>>t){
getchar();
while(t--){
getline(cin,str);pre=pos=;//初始定位为0
while((pos=str.find(" ",pos))!=string::npos){
tmp=str.substr(pre,pos-pre);//每次从pos位置开始截取pos-pre个字符
reverse(tmp.begin(),tmp.end());//反转字符串
cout<<tmp<<' ';//输出并且带空格输出
pre=++pos;//pre指向下一个位置
}
tmp=str.substr(pre);//获取最后的一个单词
reverse(tmp.begin(),tmp.end());//反转字符串
cout<<tmp<<endl;//直接输出并且换行
}
}
return ;
}

题解报告:hdu 1062 Text Reverse的更多相关文章

  1. hdu 1062 Text Reverse 字符串

    Text Reverse                                                                                  Time L ...

  2. HDU 1062 Text Reverse(水题,字符串处理)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1062 解题报告:注意一行的末尾可能是空格,还有记得getchar()吃回车符. #include< ...

  3. HDOJ/HDU 1062 Text Reverse(字符串翻转~)

    Problem Description Ignatius likes to write words in reverse way. Given a single line of text which ...

  4. [hdu 1062] Text Reverse | STL-stack

    原题 题目大意: t组数据,每组为一行,遇到空格时讲前面的单词反转输出. 题解: 显然的栈题,遇到空格时将当前栈输出清空即可 #include<cstdio> #include<st ...

  5. HDU 1062 Text Reverse

    题意 : 给出你一个句子,让你把句子中每个单词的字母顺序颠倒一下输出. 思路 : 用栈即可,就是注意原来在哪儿有空格就要输出空格. //hdu1062 #include <iostream> ...

  6. HDOJ 1062 Text Reverse

    Text Reverse Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  7. 【HDOJ】1062 Text Reverse

    Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignati ...

  8. 1062 Text Reverse

    http://acm.hdu.edu.cn/showproblem.php?pid=1062 思路: 最主要的是通过getline函数存取字符串. 如何读取单个单词,并且反向输出? 用\n作为单个单词 ...

  9. 简单字符串处理 hdu1062 Text Reverse

    虽然这个题目一遍AC,但是心里还是忍不住骂了句shit! 花了一个小时,这个题目已经水到一定程度了,但是我却在反转这个操作上含糊不清,并且还是在采用了辅助数组的情况下,关系的理顺都如此之难. 其实我是 ...

随机推荐

  1. Eureka 简介

    Eureka 简介

  2. centos Crontab

    minute   hour   day   month   week   command     顺序:分 时 日 月 周 命令 第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4 ...

  3. INTERSECT(交集)集合运算

    在集合论中,两个集合(记为集合A和B)的交集是由既属于A,也属于B的所有元素组成的集合. 在T-SQL 中,INTERSECT 集合运算对两个输入查询的结果集取其交集,只返回在两个查询结果集中都出现的 ...

  4. InfoQ中文站特供稿件:Rust编程语言的核心部件

    本文为InfoQ中文站特供稿件.首发地址为: http://www.infoq.com/cn/articles/rust-core-components .如需转载.请与InfoQ中文站联系. 原文发 ...

  5. strtok函数

    strtok函数是cstring文件里的函数 strtok函数是cstring文件里的函数 其功能是截断字符串 原型为:char *strtok(char s[],const char *delin) ...

  6. RSA私钥加密公钥解密、各种密钥格式转换

    此随笔解决RSA加解密相关的3个问题,详情可以查看源码. 1.公钥加密.私钥解密2.各种格式RSA密钥之间的转换3.不限制加密原文的长度

  7. pip 安装速度慢解决办法

    https://blog.csdn.net/liujingclan/article/details/50176597 https://blog.csdn.net/rytyy/article/detai ...

  8. 浅谈JavaScript的面向对象程序设计(三)

    前面已经对JavaScript的面向对象程序设计作了简单的介绍,包括了对象的属性.对象的工厂模式.构造函数和原型等.通过介绍,这些创建对象的方法依然有不少优化和改进的地方. 组合使用构造函数模式和原型 ...

  9. 【iOS-Tips】-小贴士

    [iOS-Tips]-小贴士 1:UIImage的2种加载方式: 方式一:有缓存(图片所占用的内存会一直停留在程序中) //name是图片的文件名 + (UIImage *)imageNamed:(N ...

  10. ie6不支持png图片的解决办法

    在head里引入png.js文件 <!--[if lte IE 6]> <script type="text/javascript" src="js/P ...