HDU1062 Text Reverse【水题】

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语言程序如下:

/* HDU1062 Text Reverse */

#include <stdio.h>

#define MAXSTACK 1024

char stack[MAXSTACK];
int pstack;

void push(char c)
{
    stack[pstack++] = c;
}

char pop()
{
    return stack[--pstack];
}

int main(void)
{
    int t;
    char c;

    scanf("%d", &t);
    getchar();
    while(t--) {
        pstack = 0;

        for(;;) {
            c = getchar();
            if(c == ' ' || c == '\n') {
                while(pstack)
                    putchar(pop());
                putchar(c);
            } else
                push(c);

            if(c == '\n')
                break;
        }
    }

    return 0;
}


  • 6
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值