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.

扫描二维码关注公众号,回复: 3514435 查看本文章

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.

自己写的代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char s[100000];
char a[100000];
int main()
{
	int T;
	cin>>T;
	getchar();
	while(T--) {
		gets(s);
		strcpy(a,s);
		char * pa=a;
		while(*pa==' ') {
			pa++;
		}
		char * p=NULL;
		char word[50];
		p=strtok(s," ");
		while(p) {
			strcpy(word,p);
			int l=strlen(word);
			for(int i=0; 2*i<l; i++) {
				swap(word[i],word[l-i-1]);
			}
			for(int i=0; i<l; i++) {
				*(pa++)=word[i];
			}
			while(*pa==' ') {
				pa++;
			}
			memset(word,0,sizeof(word));
			p=strtok(NULL," ");
		}
		puts(a);
	}
}

模拟栈的写法:


 
#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;
}

用C++调用栈

#include<stdio.h>
#include<stack>
using namespace std;
int main()
{
	int n;
    char ch;
	scanf("%d",&n);
	getchar(); /*吸收回车符*/
	while(n--)
	{
		stack<char> s; /*定义栈*/
		while(true)
		{
			ch=getchar(); /*压栈时,一次压入一个字符*/
            if(ch==' '||ch=='\n'||ch==EOF)
			{
				while(!s.empty())
				{
					printf("%c",s.top()); 
					s.pop(); /*清除栈顶元素*/
				}
				if(ch=='\n'||ch==EOF)  
					break;  /*绝对不能少,控制输出结束*/
				printf(" ");
			}
			else
				s.push(ch);
		}
		printf("\n");
	}
	return 0;
}

普通做法:

#include<stdio.h>
#include<string.h>
int main()
{
    int i,n,len,j,k,t;
    char s1[1005],s2[100];
    scanf("%d",&n);
    getchar();
    while(n--)
    {
        gets(s1);
        len=strlen(s1);
        for(i=0,j=0,t=0;i<len;i++)
        {
            if(s1[i]!=' ')
                s2[j++]=s1[i]; /*保存单词*/
            else
            {
                if(t>0) printf(" "); /*控制格式*/
                for(k=j-1;k>=0;k--) 
                    printf("%c",s2[k]); /*反转输出*/
                j=0;
                t++;
            }
            if(i==len-1) /*反转最后一个单词,这里要特别注意*/
            {
                printf(" ");
                for(k=j-1;k>=0;k--)
                    printf("%c",s2[k]);
            }
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41667282/article/details/81331604