简体   繁体   中英

Can someone help me with Recursive Function in c++?

Can someone help me understand Recursive Function?

I've this exercise that tells me to discover if the first half of a number is the same of the other. Example(123123)

#include <iostream>
using namespace std;

bool half(int arr[], int inf, int sup)
{
   if(inf >= sup)
      return true;
   if(arr[inf] != arr[sup])
      return false;
   else
      return half(arr, inf+1, sup-1);
}

int main()
{
    int n;
    cin >> n;

    int arr[n];
    for(int i=0; i<n; i++)
    {
        cin >> arr[i];
    }


    if(half)
        cout << "YES";
    else
        cout << "NO";
}

Thanks for your time, and i you have something simpler in mind, just tell!

if(half)

should be

if (half(arr, 0, n))

Functions (recursive or not) don't work unless you call them.

Also your code is checking if an array is a palindrome eg {1,2,3,3,2,1}, it doesn't do what you are asking for in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM