使用递归的数组的最小值.这是怎么回事
Minimum of an array using recursion. How does this work?
请有人解释一下下面的代码是如何工作的?
int minElement(int arr[], int n) {
if(n == 1)
return arr[0];
else {
int m = minElement(arr, n-1);
if(m < arr[n-1])
return m;
else
return arr[n-1];
}
}
我评论了每一行
int minElement(int arr[], int n) {
if(n == 1) //When you reach the beginning of the array
return arr[0]; // Return the first element
else {
int m = minElement(arr, n-1); // See what the minimum spot is below n-1 index
if(m < arr[n-1]) // If the min element is below you return the min element
return m;
else
return arr[n-1]; // If not return your value as the min element up until the index
} // Repeat till you reach the top again
}
希望这能帮助
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用递归的数组的最小值.这是怎么回事
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 有没有办法使用递归函数找到数组中最小值的 INDEX?C++
- 程序以使用 C++ 中的 while 循环查找一组数字的最小值
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- txt 文件中浮点数的最大和最小值
- 余数运算符的等效操作,用于处理低于允许的最小值
- 需要使用模板查找数组的第二个最小和最小值
- CUDA - 将 float3 数组的 (x,y,z) 分量的最小值/最大值分开?
- 查找包含 N 个元素的数组的最小值和最大值
- 错误C++在Visual Studio 2019中使用numeric_limits的长双精度最小值/最大值
- 是否存在用于 C 或 C++ 中常见数学运算(例如最小值、最大值和平均值)的可导入库?
- 如何在 c++ 中创建对的优先级队列.这会弹出具有最小值的元素.默认的弹出最大值
- 将最小值整数转换为无符号长整型
- 查找 std::vector 中的最小值和最大值 std::p air
- 如何找到每行的最大值和最小值
- 在不进行排序的情况下查找数组中n个最小值的索引