下面的代码是正确的递归代码吗?

时间:2017-09-09 20:12:40

标签: c++ recursion

#include<iostream>
using namespace std;
int Largest(int arr[], int start, int end,int max) {

if (end > 0)
{
    if (arr[end] > arr[end - 1])
    {
        if (arr[end - 1] > max)
        {
            max = arr[end];
            Largest(arr,start,end-1,max);
        }
        else
            Largest(arr,start,end-1,max);
    }
    else
    {
        if (arr[end - 1] > max)
        {
            max = arr[end-1];
            Largest(arr,start,end-1,max);
        }
        else
            Largest(arr,start,end-1,max);
    }
}
else
    return max;
}

int main() {
    int arr[5] = {700,999,199,200,699};
    int start = 0, end = 4, max = 0;
    int maximum = Largest(arr,start,end,max);
    cout << endl << endl << "Maximum = " << maximum << endl << endl;
}

我必须写一个递归的c ++代码来从数组中找到最大的数字。我写了这段代码,工作正常。但是,由于我是递归的新手,我不知道这个代码是对的。我很怀疑,因为我多次调用该函数。那么,请告诉我这是正确的递归代码吗?

3 个答案:

答案 0 :(得分:3)

此方法适用于您希望通过提供startIndex

进行搜索的情况
 function large(int arr[], int startIndex, int length, int ans)
    {
        if (startIndex == length - 1)
            return ans;
         else
            return large(arr, startIndex+1, length, max(ans, arr[startIndex]);
    }

function max(a,b){
    if(a>b)
        return a;
    else
        return b;
}

答案 1 :(得分:2)

它可能正常,但这不是正确的方法。应谨慎使用递归方法。见下文

int large(int arr[], int size, int largest)
{
    if (size == 1)
        return largest;

    if (size > -1)
    {
        if (arr[size] > largest)
        {
            largest = arr[size];
        }
        return(largest = large(arr, size - 1, largest));
    }
    else
    {
        return largest;
    }
}

希望这会对你有所帮助。

答案 2 :(得分:2)

也许这可以解决问题。它不是真正的 C ++,但它是一本书练习的实现:

#include <iostream>

// LARGEST
// Returns the largest nuber in an array of int
// arr: the input array
// size: the length of our array (WARNING: segmentation fault if not correct)
int largest(int arr[], int size) {
  // Let's save the head of our vector
  int head = arr[0];

  // If we have reached the end of our vector we return the head
  // and the function does not continue
  if (size == 1)
    return head;

  // If we have still something in our array we go to check
  // the largest number in the tail
  int tail = largest(arr + 1, size - 1);

  // We return the biggest between the head and tail
  return (head > tail ? head : tail);
}

int main() {
  int arr[5] = {700,999,199,200,699};
  int max = largest(arr, 5);
  std::cout << max << std::endl;
}

如果您还需要一个不同于0的起点,您可以使用:

#include <iostream>

// LARGEST
// Returns the largest nuber in an array of int
// arr: the input array
// size: the length of our array (WARNING: segmentation fault if not correct)
// start: starting point for the search (default to 0)
int largest(int* arr, int size, int start = 0) {
  // Let's save the head of our vector, from the starting point
  int head = arr[start];

  int* _arr = arr + start; // Select the real new position of the 
  size -= start; // and decrementing the size accordingly

  // If we have reached the end of our vector we return the head
  // and the function does not continue
  if (size == 1)
    return head;

  // If we have still something in our array we go to check
  // the largest number in the tail
  int tail = largest(_arr + 1, size - 1);

  // We return the biggest between the head and tail
  return (head > tail ? head : tail);
}

int main() {
  int arr[5] = {700,999,199,200,699};
  int max = largest(arr, 5, 2);
  std::cout << max << std::endl;
}