Array to achieve reverse, sequentially output

When it comes to output an array of Retrograde, I am sure you can come up with a variety of ways, such as by length, such as the use of the stack, but today I'm going to bring you an array is achieved by recursively reverse the order and output.

  • Reverse output: ah method according to a [n-1] -a [0], we can think about, we recursively, to make a final output, and then output the next recursion, is 0 when the ending condition is the len End.
  • Output order: We also use recursive implementation, we can first recursive, recursive backtracking when output, thereby bringing the output sequence, and the above sequence is not the same.

Now I use a diagram to represent the above two processes:

# include <stdio.h>
# include <stdlib.h>
void reverse(int *a,int len)
{
	if(len<=0)
	{
		return;
	}
	printf("%d ",a[len-1]);
	reverse(a,len-1);
	printf("\n");
}
void sequence(int *a,int len)
{
	if(len<=0)
	{
		return;
	}
	sequence(a,len-1);
	printf("%d ",a[len-1]);
	
}

int main()
{
	int a[5]={1,2,3,4,5};
	reverse(a,5);
	sequence(a,5);
}

Come on! 

 

Published 54 original articles · won praise 8 · views 5325

Guess you like

Origin blog.csdn.net/qq_43411555/article/details/90267606