c-moodle/homework/11961.c

32 lines
1.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <stdio.h>
int x; // 用于记录输入的数字
int n; // 数组中元素的个数
int a[100005]; // 定义整型数组,存储输入的数字
// 递归函数,求数组元素的最大值
// 参数1目前找到的最大数参数2当前比较的元素下标
int find(int max_num, int index) {
// 如果遍历到了数组最后一个元素,返回目前找到的最大数
if (index == n)
return max_num;
// 如果当前元素比最大数还要大,在下标加一的位置继续查找
if (a[index] > max_num)
return find(a[index], index + 1);
// 如果当前元素比最大数小,就在下标加一的位置继续查找
else
return find(max_num, index + 1);
}
int main() {
// 循环输入数字,并存储到数组中,直到输入-9999为止
while (scanf("%d", &x) != EOF) {
if (x == -9999)
break;
a[n++] = x;
}
// 调用递归函数,求出数组元素的最大值,并输出
printf("%d", find(-__INT_MAX__, 0));
return 0;
}