ZOJ——1151 Word Reversal

逃离我推掉我的手 2021-09-26 12:28 271阅读 0赞

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1151

题目:

Word Reversal


Time Limit: 2 Seconds Memory Limit: 65536 KB


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

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.

Input

You will be given a number of test cases. 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

1

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

题目描述:

  1. 简单的字符串处理,给出一行单词,把这里面的单词翻转后在输出。单词之间可能不止有一个空格,别问我怎么知道的,,

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. void qe(char *aw)
  4. {
  5. int i;
  6. for(i = strlen(aw) - 1;i >=0;i--)
  7. putchar(aw[i]);
  8. }
  9. int main()
  10. {
  11. int t,i,j,k;
  12. int a,b,c;
  13. while(scanf("%d",&a)!=EOF){
  14. k = 0;
  15. while(a--){
  16. if(k)
  17. putchar('\n');
  18. scanf("%d",&b);
  19. char *f = NULL;
  20. char we[10010],ww[100];
  21. gets(we);
  22. for(i = 0 ;i < b;i ++){
  23. gets(we);
  24. for(t = 0,f = we;f[0] != '\0';t ++){
  25. if(f[0] != ' '){
  26. sscanf(f,"%s",ww);
  27. qe(ww);
  28. f = f + strlen(ww);
  29. }
  30. else{
  31. printf("%c",f[0]);
  32. f = f + 1;
  33. }
  34. }
  35. putchar('\n');
  36. }
  37. k++;
  38. }
  39. }
  40. return 0;
  41. }

发表评论

表情:
评论列表 (有 0 条评论,271人围观)

还没有评论,来说两句吧...

相关阅读

    相关 ZOJ 1295——Reverse Text

    在没学STL之前,可能做这道题需要开数组,在出现空格的地方外加判断,但是学完string,一条reverse语句便将输入逆转,c语音可能需要2-30行,可见c++的优化。注意: