zoukankan      html  css  js  c++  java
  • sicily 1323. Switch text

    Description
    The program must switch the text lines in a backward order and split them by the middle, processing the input lines in pairs. If an empty or blank line is found, it is considered as a line but it is not printed out to the output.

    Input
    The input will be a text file with text lines.

    Output
    Standard output with the switched and splitted lines.

    题目理解有点困难。拿样例输入的两句做题解,就是这样的

    This lines must be printed backwards and splitted in the middle.
    And each line too!
    ↓两行交换
    And each line too!
    This lines must be printed backwards and splitted in the middle.
    ↓字母倒转
    !oot enil hcae dnA
    elddim eht ni dettilps dna sdrawkcab detnirp eb tsum senil sihT.
    ↓分成两半
    !oot enil/*分开*/ hcae dnA
    elddim eht ni dettilps dna sdra/*分开*/wkcab detnirp eb tsum senil sihT.
    ↓两半调转顺序,输出
     hcae dnA!oot enil
    wkcab detnirp eb tsum senil sihT.elddim eht ni dettilps dna sdra

    注意奇数长度的字符串的分割方法,设长度为2k+1,输出的第一半就是k+1~1,第二半是2k+1~k+2

    虽然题目没有讲,但是是用EOF结束输出的。还有就是空行的判断方法要留点心,因为题目里说空行有两种,一是全空格,一是连空格都没有,直接回车。

    有两种办法做,可以开数组,先倒转,然后将两半错开填进去,比较好理解,也比较啰嗦

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define SIZE 10001
     4 int isEmpty ( char str[] );
     5 void switchAndSplit( char str[] );
     6 void printString( char str[], int len );
     7 int main()
     8 {
     9     char str1[SIZE] = {0};
    10     char str2[SIZE] = {0};
    11     while ( gets(str1) )
    12     {
    13         gets(str2);
    14         switchAndSplit(str2);
    15         switchAndSplit(str1);
    16     }
    17     return 0;
    18 }
    19 
    20 int isEmpty ( char str[] )
    21 {
    22     int len, i;
    23     
    24     len = strlen(str);
    25     
    26     for ( i = 0; i < len; i++ )
    27     {
    28         if ( str[i] != ' ' )
    29         {
    30             return 0;
    31         }
    32     }
    33     
    34     return 1;
    35 }
    36 
    37 void switchAndSplit( char str[] )
    38 {
    39     int len;
    40     int i, j;
    41     char temp[SIZE] = {0};
    42     char part1[SIZE] = {0};
    43     char part2[SIZE] = {0};
    44     char switched[SIZE] = {0};
    45     char splited[SIZE] = {0};
    46     
    47     len = strlen(str);
    48     
    49     if ( isEmpty(str) == 0 )
    50     {
    51         for ( i = 0; i < len; i++ )
    52         {
    53             switched[i] = str[len - i - 1];
    54         } 
    55         
    56         j = 0;
    57         
    58         for ( i = len / 2; i < len; i++ )
    59         {
    60             splited[j] = switched[i];
    61             j++;
    62         }
    63         
    64         for ( i = 0; i < len / 2; i++ )
    65         {
    66             splited[j] = switched[i];
    67             j++;
    68         }
    69         
    70         printString( splited, j );
    71     }
    72     
    73 }
    74 
    75 void printString( char str[], int len )
    76 {
    77     int i;
    78     
    79     for ( i = 0; i < len; i++ )
    80     {
    81         printf( "%c", str[i] ); 
    82     }
    83     
    84     printf("\n");
    85 }


    第二种是用指针做,只是改变输出字符的顺序,比较简洁,但是分割点的设置比较烦(也可能只是我设的比较烦……)

    View Code
     1 #include<stdio.h>
     2 #include<string.h>
     3 #define SIZE 10001
     4 int isEmpty ( char str[] );
     5 void switchAndSplit( char str[] );
     6 void printString( char str[], int len );
     7 int main()
     8 {
     9     char str1[SIZE] = {0};
    10     char str2[SIZE] = {0};
    11     while ( gets(str1) )
    12     {
    13         gets(str2);
    14         switchAndSplit(str2);
    15         switchAndSplit(str1);
    16     }
    17     return 0;
    18 }
    19 
    20 int isEmpty ( char str[] )
    21 {
    22     int len, i;
    23     
    24     len = strlen(str);
    25     
    26     for ( i = 0; i < len; i++ )
    27     {
    28         if ( str[i] != ' ' )
    29         {
    30             return 0;
    31         }
    32     }
    33     
    34     return 1;
    35 }
    36 
    37 void switchAndSplit( char str[] )
    38 {
    39     int len;
    40     int i, j;
    41     char temp[SIZE] = {0};
    42     char part1[SIZE] = {0};
    43     char part2[SIZE] = {0};
    44     char switched[SIZE] = {0};
    45     char splited[SIZE] = {0};
    46     
    47     len = strlen(str);
    48     
    49     if ( isEmpty(str) == 0 )
    50     {
    51         for ( i = 0; i < len; i++ )
    52         {
    53             switched[i] = str[len - i - 1];
    54         } 
    55         
    56         j = 0;
    57         
    58         for ( i = len / 2; i < len; i++ )
    59         {
    60             splited[j] = switched[i];
    61             j++;
    62         }
    63         
    64         for ( i = 0; i < len / 2; i++ )
    65         {
    66             splited[j] = switched[i];
    67             j++;
    68         }
    69         
    70         printString( splited, j );
    71     }
    72     
    73 }
    74 
    75 void printString( char str[], int len )
    76 {
    77     int i;
    78     
    79     for ( i = 0; i < len; i++ )
    80     {
    81         printf( "%c", str[i] ); 
    82     }
    83     
    84     printf("\n");
    85 }
  • 相关阅读:
    codeforces 1096 题解
    pkuwc 前的任务计划
    codeforces 1093 题解
    luoguP5068 [Ynoi2015]我回来了
    luoguP5074 Eat the Trees
    二分
    保护
    数数字
    旅行
    すすめ!
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/2813541.html
Copyright © 2011-2022 走看看