首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

HDU1062 Text Reverse【水题】

  • 24-03-03 15:22
  • 3650
  • 6722
blog.csdn.net

Text Reverse

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34466    Accepted Submission(s): 13535


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.

问题链接:HDU1062 Text Reverse。

问题简述:

  输入测试例子数量t,然后输入t行字符串,将每一行的每一个单词逆序后输出该行的语句(字符串)。

问题分析:

  利用堆栈后进先出的原理,逆序处理可以使用堆栈来实现。每一个单词是用空格或行结束符隔开的。每行的处理到换行符为止。

程序说明:

  使用了一个自行实现的堆栈,简单地实现逆序功能。本程序使用getchar()函数处理输入流,除了输入字符压栈外,读入的字符直接输出输出,没有使用多余的缓存。


AC通过的C语言程序如下:

  1. /* HDU1062 Text Reverse */
  2. #include
  3. #define MAXSTACK 1024
  4. char stack[MAXSTACK];
  5. int pstack;
  6. void push(char c)
  7. {
  8. stack[pstack++] = c;
  9. }
  10. char pop()
  11. {
  12. return stack[--pstack];
  13. }
  14. int main(void)
  15. {
  16. int t;
  17. char c;
  18. scanf("%d", &t);
  19. getchar();
  20. while(t--) {
  21. pstack = 0;
  22. for(;;) {
  23. c = getchar();
  24. if(c == ' ' || c == '\n') {
  25. while(pstack)
  26. putchar(pop());
  27. putchar(c);
  28. } else
  29. push(c);
  30. if(c == '\n')
  31. break;
  32. }
  33. }
  34. return 0;
  35. }


注:本文转载自blog.csdn.net的海岛Blog的文章"http://blog.csdn.net/tigerisland45/article/details/51940349"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2024 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top