• HDUOJ--------Text Reverse


     

    Text Reverse

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13449    Accepted Submission(s): 5140
    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 ' ' after the interger T, then you may use gets() to read a line and process it.

     基础题:

    代码:

     1 #include<iostream>
     2 #define maxn 1005
     3 using namespace std;
     4 char str[maxn][maxn];
     5 int main()
     6 {
     7     int n,i,cnt,j;
     8     char c;
     9     bool ifhave;
    10     //freopen("test.in","r",stdin);
    11     //freopen("teset.out","w",stdout);
    12     scanf("%d",&n);
    13     getchar();
    14     while(n--)
    15     {
    16         j=i=0;
    17         ifhave=false;
    18      while(c=getchar(),c!='
    ')
    19      {
    20          if(c==' ')
    21              j++,i=0;
    22          else
    23             str[j][i++]=c;
    24          
    25      }
    26      if(c==' ')
    27          ifhave=true,j--;
    28      cnt=j;
    29      for(j=0;j<=cnt;j++)
    30      {
    31          for(i=strlen(str[j])-1; i>=0 ; i--)
    32              printf("%c",str[j][i]);
    33             if(j!=cnt) printf(" ");
    34      }
    35      if(ifhave)printf(" ");
    36      puts("");
    37      memset(str,'',sizeof str);
    38     }
    39     return 0;
    40 }

    比较高级的代码:

     1 #include<stdio.h>
     2 #include<string.h>
     3 int main()
     4 {
     5   int t;
     6   scanf("%d
    ",&t);
     7   while(t--)
     8   {
     9     char s[1005]={};
    10     while(scanf("%[^ 
    ]",s)!=EOF)
    11     {
    12       printf("%s",strrev(s));
    13       s[0]=0;
    14       putchar(getchar());
    15     }
    16   }
    17   return 0;
    18 }
  • 相关阅读:
    SQL(二)语法
    SQL(一)简介
    linux学习(六)文件基本属性
    Linux学习(五)远程登录
    Linux学习(四) 忘记密码解决方法
    怎样理解阻塞非阻塞与同步异步的区别?
    Python的lambda匿名函数
    Twisted源码分析系列01-reactor
    Python装饰器学习(九步入门)
    Python关键字yield的解释
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3306347.html
Copyright © 2020-2023  润新知