程序设计题(要求用递归函数实现) 1.编写递归函数,在一个整型数组中求最大值。c语言
时间: 2024-06-14 19:03:42 浏览: 16
以下是用递归函数实现在一个整型数组中求最大值的C语言代码:
```c
#include <stdio.h>
int findMax(int arr[], int n) {
// 如果数组中只有一个元素,直接返回该元素
if (n == 1) {
return arr[0];
}
// 递归调用函数,求出除最后一个元素外的数组的最大值
int max = findMax(arr, n - 1);
// 比较最后一个元素和前面求出的最大值,返回较大的那个
return (arr[n - 1] > max) ? arr[n - 1] : max;
}
int main() {
int arr[] = {10, 324, 45, 90, 9808};
int n = sizeof(arr) / sizeof(arr[0]);
printf("数组中的最大值为:%d", findMax(arr, n));
return 0;
}
```
相关问题
用C语言写一个递归求整型数组中前n个元素的最大值的函数
int max(int array[],int n)
{
if(n == 1)
return array[0];
else
{
int m = max(array, n-1);
if(m > array[n-1])
return m;
else
return array[n-1];
}
}
c语言中输入数组并用递归函数输出其最大值
这是一个Python的问题,意思是输入一个数组,然后用递归函数输出它的最大值。
答案:可以使用递归函数来找到数组中的最大值。首先,定义一个函数,该函数使用递归方式遍历数组和找到最大值。然后在主函数中调用该函数,并将数组作为参数传递给函数。函数会在数组中找到最大值,并将其返回给主函数。主函数会输出该值。例如:
```python
def findMax(arr, n):
# Base case
if n == 1:
return arr[0]
return max(arr[n-1], findMax(arr, n-1))
# Driver code
arr = [10, 324, 45, 90, 9808]
n = len(arr)
print("最大值是", findMax(arr, n))
```
运行结果:
```
最大值是 9808
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)