Blue Bridge Cup ADV-9 recursive algorithm to improve the array of characters upside down

Recursive algorithm to improve the array of characters inverted  
time limit: 1.0s memory limit: 512.0MB

description of the problem
  to complete a recursive procedure, inverted array of characters. And the printing process to achieve
  the recursive logic:
  when the character length is equal to 1, direct return
  Otherwise, change and last two characters, character array inverted in the remainder of recursively

 

Input format
  character array and the array length

 

Output format
  in the solution process, the print changes in the array of characters.
  Finally a blank line, each element of the array at the end of the program after printing upside down.

 

Sample input
the Sample. 1
. 5 ABCDE
the Sample 2
. 1 A

 

Output Sample
the Sample. 1
ebcda
EDCBA
EDCBA
the Sample 2
A
 

#include <stdio.h>

int len;
char str[1005];

void reverse(int begin, int end)
{
    if (begin >= end)
        printf("\n%s", str);
    else
    {
        char temp = str[begin];
        str[begin] = str[end];
        str[end] = temp;
        printf("%s\n", str);
        reverse(begin+1, end-1);
    }
}

int main()
{
    scanf("%d %s", &len, str);

    reverse(0, len-1);

    return 0;
}

 

Published 223 original articles · won praise 40 · views 40000 +

Guess you like

Origin blog.csdn.net/liulizhi1996/article/details/104089385