📜  递归练习问题套装1

📅  最后修改于: 2021-05-07 18:14:27             🧑  作者: Mango

解释以下功能的功能。

问题1

C++
int fun1(int x, int y)
{
    if (x == 0)
        return y;
    else
        return fun1(x - 1, x + y);
}


C
int fun1(int x, int y)
{
    if (x == 0)
        return y;
    else
        return fun1(x - 1, x + y);
}


Java
static int fun1(int x, int y)
{
    if (x == 0)
        return y;
    else
        return fun1(x - 1, x + y);
}


Python3
def fun1(x, y) :
 
    if (x == 0) :
        return y
    else :
        return fun1(x - 1, x + y)
 
# This code is contributed by divyesh072019


C#
static int fun1(int x, int y)
{
    if (x == 0)
        return y;
    else
        return fun1(x - 1, x + y);
}
 
// This code is contributed by divyesh072019


Javascript


C++
// minimum index finder
 
int minIndex(int arr[], int s, int e)
{
    int sml = INT32_MAX;
    int mindex;
    for (int i = s; i < e; i++) {
        if (sml > arr[i]) {
            sml = arr[i];
            mindex = i;
        }
    }
    return mindex;
}
 
void fun2(int arr[], int start_index, int end_index)
{
    if (start_index >= end_index)
        return;
    int min_index;
    int temp;
 
    // minIndex() returns index of minimum value in
    // array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index);
 
    // swap the element at start_index and min_index
    swap(arr[start_index], arr[min_index]);
 
    fun2(arr, start_index + 1, end_index);
}
 
// This code is contributed by nishant_0073


C
// minimum index finder
 
int minIndex(int arr[], int s, int e)
{
    int sml = INT32_MAX;
    int mindex;
    for (int i = s; i < e; i++) {
        if (sml > arr[i]) {
            sml = arr[i];
            mindex = i;
        }
    }
    return mindex;
}
 
void fun2(int arr[], int start_index, int end_index)
{
    if (start_index >= end_index)
        return;
    int min_index;
    int temp;
 
    // minIndex() returns index of minimum value in
    // array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index);
 
    temp = arr[start_index];
    arr[start_index] = arr[min_index];
    arr[min_index] = temp;
 
    fun2(arr, start_index + 1, end_index);
}


Java
// minimum index finder
static int minIndex(int arr[], int s, int e)
{
    int sml = Integer.MAX_VALUE;
    int mindex = ;
    for (int i = s; i < e; i++) {
        if (sml > arr[i]) {
            sml = arr[i];
            mindex = i;
        }
    }
    return mindex;
}
 
static void fun2(int arr[], int start_index, int end_index)
{
    if (start_index >= end_index)
        return;
    int min_index;
    int temp;
 
    // minIndex() returns index of minimum value in
    //   array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index);
 
    temp = arr[start_index];
    arr[start_index] = arr[min_index];
    arr[min_index] = temp;
 
    fun2(arr, start_index + 1, end_index);
}
 
// This code is contributed by nishant_0073


Python3
# Minimum index finder
def minIndex(arr, s, e):
     
    sml = sys.maxsize
    mindex = 0
     
    for i in range(s, e):
        if (sml > arr[i]):
            sml = arr[i]
            mindex = i
             
    return mindex
 
def fun2(arr, start_index, end_index):
     
    if (start_index >= end_index):
        return
         
    # minIndex() returns index of minimum value in
    # array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index)
    arr[start_index], arr[min_index] = arr[min_index], arr[start_index]
    fun2(arr, start_index + 1, end_index)
 
# This code is contributed by rag2127


C#
// minimum index finder
static int minIndex(int[] arr, int s, int e)
{
    int sml = Int32.MaxValue;
    int mindex;
    for(int i = s; i < e; i++)
    {
        if(sml > arr[i])
        {
            sml = arr[i];
            mindex = i;
        }
    }
    return mindex;  
}
static void fun2(int[] arr, int start_index, int end_index)   
{
    if(start_index >= end_index)
    {
        return;
    }
    int min_index;
    int temp;
   
    // minIndex() returns index of minimum value in
    //   array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index);
    temp = arr[start_index];
    arr[start_index] = arr[min_index];
    arr[min_index] = temp;
  
    fun2(arr, start_index + 1, end_index);
}
 
// This code is contributed by avanitrachhadiya2155


答: fun()函数计算并返回x(x + 1)/ 2 + y((1 + 2…+ x-1 + x)+ y)。例如,如果x为5且y为2,则fun应该返回15 + 2 = 17。

问题2

C++

// minimum index finder
 
int minIndex(int arr[], int s, int e)
{
    int sml = INT32_MAX;
    int mindex;
    for (int i = s; i < e; i++) {
        if (sml > arr[i]) {
            sml = arr[i];
            mindex = i;
        }
    }
    return mindex;
}
 
void fun2(int arr[], int start_index, int end_index)
{
    if (start_index >= end_index)
        return;
    int min_index;
    int temp;
 
    // minIndex() returns index of minimum value in
    // array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index);
 
    // swap the element at start_index and min_index
    swap(arr[start_index], arr[min_index]);
 
    fun2(arr, start_index + 1, end_index);
}
 
// This code is contributed by nishant_0073

C

// minimum index finder
 
int minIndex(int arr[], int s, int e)
{
    int sml = INT32_MAX;
    int mindex;
    for (int i = s; i < e; i++) {
        if (sml > arr[i]) {
            sml = arr[i];
            mindex = i;
        }
    }
    return mindex;
}
 
void fun2(int arr[], int start_index, int end_index)
{
    if (start_index >= end_index)
        return;
    int min_index;
    int temp;
 
    // minIndex() returns index of minimum value in
    // array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index);
 
    temp = arr[start_index];
    arr[start_index] = arr[min_index];
    arr[min_index] = temp;
 
    fun2(arr, start_index + 1, end_index);
}

Java

// minimum index finder
static int minIndex(int arr[], int s, int e)
{
    int sml = Integer.MAX_VALUE;
    int mindex = ;
    for (int i = s; i < e; i++) {
        if (sml > arr[i]) {
            sml = arr[i];
            mindex = i;
        }
    }
    return mindex;
}
 
static void fun2(int arr[], int start_index, int end_index)
{
    if (start_index >= end_index)
        return;
    int min_index;
    int temp;
 
    // minIndex() returns index of minimum value in
    //   array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index);
 
    temp = arr[start_index];
    arr[start_index] = arr[min_index];
    arr[min_index] = temp;
 
    fun2(arr, start_index + 1, end_index);
}
 
// This code is contributed by nishant_0073

Python3

# Minimum index finder
def minIndex(arr, s, e):
     
    sml = sys.maxsize
    mindex = 0
     
    for i in range(s, e):
        if (sml > arr[i]):
            sml = arr[i]
            mindex = i
             
    return mindex
 
def fun2(arr, start_index, end_index):
     
    if (start_index >= end_index):
        return
         
    # minIndex() returns index of minimum value in
    # array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index)
    arr[start_index], arr[min_index] = arr[min_index], arr[start_index]
    fun2(arr, start_index + 1, end_index)
 
# This code is contributed by rag2127

C#

// minimum index finder
static int minIndex(int[] arr, int s, int e)
{
    int sml = Int32.MaxValue;
    int mindex;
    for(int i = s; i < e; i++)
    {
        if(sml > arr[i])
        {
            sml = arr[i];
            mindex = i;
        }
    }
    return mindex;  
}
static void fun2(int[] arr, int start_index, int end_index)   
{
    if(start_index >= end_index)
    {
        return;
    }
    int min_index;
    int temp;
   
    // minIndex() returns index of minimum value in
    //   array arr[start_index...end_index]
    min_index = minIndex(arr, start_index, end_index);
    temp = arr[start_index];
    arr[start_index] = arr[min_index];
    arr[min_index] = temp;
  
    fun2(arr, start_index + 1, end_index);
}
 
// This code is contributed by avanitrachhadiya2155

答:函数fun2()是选择排序的递归实现。