32 lines
1.0 KiB
C
32 lines
1.0 KiB
C
#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;
|
||
}
|