zoukankan      html  css  js  c++  java
  • Word Reversal

    Description

    For each list of words, output a line with each word reversed without changing the order of the words.

    Input

    The first line contains a positive integer indicating the number of cases to follow. Each case is given on a line containing a list of words separated by one space, and each word contains only uppercase and lowercase letters.

    Output

    For each test case, print the output on one line.

    Sample Input

    3
    I am happy today
    To be or not to be
    I want to win the practice contest
    

    Sample Output

    I ma yppah yadot
    oT eb ro ton ot eb
    I tnaw ot niw eht ecitcarp tsetnoc



    题目意思:将每个单词的字符进行反转。


    最初代码:
    #include<stdio.h>
    #include<string.h>
    int main()
    {
        int n,m,i,j,k;
        char s[1000],x[1000];
        scanf("%d",&n);
        getchar();
        while(n--)
        {
            gets(s);
            m=strlen(s);
            k=0;
            for(i=0; i<=m; i++)
            {
                if(s[i]!=' '&&s[i]!=0)
                {
                    x[k]=s[i];
                    k++;
                }
                else
                {
                    for(j=k-1; j>=0; j--)
                        printf("%c",x[j]);///逆向输出
                    if(s[i]!=0)
                        printf(" ");
                    k=0;
                }
            }
    
            printf("
    ");
        }
    
        return 0;
    }
    
    

    这个代码是一个数组向另外一个数组中倒数,时间复杂度上还是存在着问题,修改之后的新代码如下:

     1 #include<stdio.h>///时间复杂度更小的另外一种方法
     2 #include<string.h>
     3 int main()
     4 {
     5     int t,m,i,j;
     6     char s[10000];
     7     scanf("%d",&t);
     8     getchar();
     9     while(t--)
    10     {
    11         gets(s);
    12         m=strlen(s);
    13         for(i=0; i<m; i++)
    14         {
    15             if(s[i]==' ')
    16             {
    17                 for(j=i-1; j>=0&&s[j]!=' '; j--)
    18                 {
    19                     printf("%c",s[j]);
    20                 }
    21                 printf(" ");
    22             }
    23         }
    24         for(i=m-1; i>=0&&s[i]!=' '; i--)
    25         {
    26             printf("%c",s[i]);
    27         }
    28         printf("
    ");
    29     }
    30     return 0;
    31 }


  • 相关阅读:
    pinfinder
    华为方舟编译器
    SSH安全加固
    KindEditor
    SQL SERVER 常见SQL和函数使用
    SQL 时间处理
    sqlSQL2008如何创建定时作业(代理服务)(转)
    登录之问题总结
    文件操作(增删查改)
    SQL2008安装后激活方式以及提示评估期已过解决方法(转)
  • 原文地址:https://www.cnblogs.com/wkfvawl/p/8733862.html
Copyright © 2011-2022 走看看