使用递归函数反转数组的代码,该函数应该将数组长度作为唯一参数。 [英] Code to reverse a array using recursive function which should have length of array as its only argument.

查看:126
本文介绍了使用递归函数反转数组的代码,该函数应该将数组长度作为唯一参数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们必须编写一个代码来使用递归函数来获取数组的反转,只有我们将数组的长度作为参数传递。

数组长度,元素读取,反向打印将通过MAIN功能完成。



以下是迭代算法。如何将其转换为递归函数?



我尝试过:



We have to write a code to get a reverse of an array using recursive function to which only we pass length of array as argument.
And length of array, reading of elements, printing of reverse will be done through MAIN function.

Following is the iterative algorithm. how to convert it into a recursive function??

What I have tried:

#include<stdio.h>
int main(void){
    int a[100], n, i, temp;
    printf("Enter the number of elements you want to enter.\n=>");
    scanf("%d", &n);
    printf("\n\nEnter the numbers:- \n");
    for(i=0; i<n; i++ ){
        scanf("%d", &a[i]);
    }
    printf("\n\nThe reverse of the entered array is:- \n\n");


    for(i=0; i<n/2; i++){
        temp = a[i];
        a[i] = a[n-i-1];
        a[n-i-1] = temp;
    }


    for(i=0; i<n; i++){
     printf("%d  ", a[i]);
    }

}

推荐答案

想一想。你会怎样手动完成这个?

假设你面前的桌子上有一排硬币:

Think about it. How would you do this manually?
Assume you have a row of pennies on the desk in front of you:
ABCDEFGH



如果您获取第(8)行中的元素数量并将最后一个元素与第一个元素交换:


If you take the number of elements in the line (8) and swap the last element with the first:

HBCDEFGA
12345678

然后将计数减少一个,并将该元素与第一个非交换元素交换:

Then reduce the count by one, and swap that element with the first non-swapped element:

HGCDEFBA
12345678

继续,直到你交换的元素少于两个,你已经完成,并且它已经反转。

所以编写你的方法来获取数组的长度并使用它来生成要交换的两个索引。

然后检查是否有任何要交换的元素,如果没有,则返回。

否则,交换它们,然后调用用一个较少的值来自己。

And continue until you have less than two elements to swap, you have finished, and it's reversed.
So write your method to take the length of the array and use that to generate the two indexes to swap.
Then check if you have any elements to swap, and if not, return.
Otherwise, swap them, and call yourself with one less value.


首先不要吝啬使用数组名称和大小 - 如果你必须递归地使用几个指向第一个和最后一个的指针characte rs要交换。类似于:



First of all don't mince about using an array name and a size - if you have to do it recursively use a couple of pointers to the first and last characters to be swapped. Something like:

void reverse_range( int *first_to_swap, int *last_to_swap )
{
    if( first_to_swap < last_to_swap )
    {
        *first_to_swap ^= *last_to_swap ^= *first_to_swap ^= *last_to_swap;
        reverse_range( first_to_swap++, last_to_swap-- );
    }
}





(这是未经测试的BTW,所以如果它有问题,不要感到惊讶。同样如果你只需要一个参数或其他一些人为限制的球,那么转换代码就很容易了 - 这只是工作。)



这是有启发性的'因为如果你将它与迭代版本进行比较......





(This is untested BTW, so don't be surprised if it's buggy. Likewise if you have to only have one parameter or some other balls ache of an artificial restriction then you converting the code is easy enough - it's just work.)

This is instructive 'cause If you compare this to the iterative version...

void reverse_range( int *first_to_swap, int *last_to_swap )
{
    while( first_to_swap < last_to_swap )
    {
        *first_to_swap ^= *last_to_swap ^= *first_to_swap ^= *last_to_swap;
        first_to_swap++, last_to_swap--;
    }
}



您将获得如何通过迭代实现制作递归版本的一般模式。用条件替换while,而不是更新迭代状态再次使用更新的变量调用函数。



更重要的是你得到对面 - 如何从递归实现转换为通常更重要的迭代实现。作为递归解决方案,可以更容易地表达大量问题,但通常迭代解决方案更快。让心理工具在两者之间切换是一个很大的帮助。


you'll get a general pattern as to how to make a recursive version out of an iterative implementation. Replace the while with a conditional and instead of updating the iteration state call the function again with what would be the updated variables.

More importantly you get the opposite - how to convert from a recursive implementation to an iterative one which is often more important. Loads of problems can be expressed easier as a recursive solution but usually an iterative solution is faster. Having the mental tools to switch between the two is a big help.


这篇关于使用递归函数反转数组的代码,该函数应该将数组长度作为唯一参数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆