找出一个整型数组中的最大值
时间: 2023-11-18 11:03:13 浏览: 65
可以使用以下代码来找出一个整型数组中的最大值:
```python
def find_max(arr):
max_num = arr[0]
for num in arr:
if num > max_num:
max_num = num
return max_num
```
调用该函数,并传入一个整型数组作为参数即可找出该数组中的最大值。
相关问题
找出一维整型数组中的次最大值。
可以使用两个变量来记录最大值和次大值,遍历一遍数组即可,代码如下:
int findSecondLargest(int arr[], int n) {
int largest = arr[0], secondLargest = INT_MIN;
for(int i = 1; i < n; i++) {
if(arr[i] > largest) {
secondLargest = largest;
largest = arr[i];
} else if(arr[i] > secondLargest && arr[i] != largest) {
secondLargest = arr[i];
}
}
return secondLargest;
}
C语言找出一维整型数组中的次最大值。
好的,我知道了,请看下面的C语言代码:
```c
#include <stdio.h>
int main() {
int array[10] = {5, 3, 8, 6, 3, 9, 1, 0, 2, 7};
int max = array[0];
int second_max = array[0];
for(int i = 0; i < 10; i++) {
if(array[i] > max) {
second_max = max;
max = array[i];
} else if(array[i] > second_max && array[i] < max) {
second_max = array[i];
}
}
printf("次最大值为: %d\n", second_max);
return 0;
}
```
这个程序会找出给定整型数组中的次最大值,并将其输出。
相关推荐
![-](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)
![](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)