杭电acm1062Text Reverse

来源:互联网 发布:淘宝出售虚拟物品 编辑:程序博客网 时间:2024/06/07 11:52

Text Reverse

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 10   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

3olleh !dlrowm'I morf .udhI 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.

Author

Ignatius.L
想法:虽是水题,有点坑人
代码一:
#include<stdio.h>
#include<string.h>
char s[1010];
char s1[1010];
int main()
{
    int T;
    while(scanf("%d",&T)!=EOF)
    {
        getchar();
        while(T--)
        {
         gets(s);
         int i,j=0;int k=0;
        for(i=0;s[i]!='\0';i++)
        {
            if(s[i]==' ')
            {
                for(j=i-1;j>=k;j--)
                {
                    printf("%c",s[j]);
                }
                k=i+1;
                printf("%c",s[i]);


            }
        }
        for(i=strlen(s)-1;i>=k;i--)
        {
            printf("%c",s[i]);
        }
        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;
}