max
Time Limit : 3000/1000ms (Java/Other) Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 283 Accepted Submission(s) : 161
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
多组用例
编写一个函数,使用递归算法求一维整型数组的最大值.
函数原型如下:
int max(int array[],int n)
参数说明:array[]为整型数组,n 为项数( n>0);返回值是数组元素的最大值。
预设代码
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include <stdio.h>
#define N 100
int max(int array[],int n);
main( )
{
int num[N],count,i,val;
while(scanf("%d",&count)!=EOF){
for (i=0;i<count;i++)
{
scanf("%d",&num[i]);
}
val=max(num,count);
printf("%d\n",val);
}
}
注意:提交时提交整个程序!
Sample Input
5 1 5 6 4 2
Sample Output
6
Author
CWIND
/* PRESET CODE BEGIN - NEVER TOUCH CODE BELOW */
#include <stdio.h>
#define N 100
int max(int array[],int n);
main( )
{
int num[N],count,i,val;
while(scanf("%d",&count)!=EOF){
for (i=0;i<count;i++)
{
scanf("%d",&num[i]);
}
val=max(num,count);
printf("%d\n",val);
}
}
int max(int array[],int n)
{
if (n>2)
if (array[n-1]>max(array,n-1))
return array[n-1];
else
return max(array,n-1);
else
if (array[0]>=array[1])
return array[0];
else
return array[1];
}
转载请注明原文地址: https://ju.6miu.com/read-700422.html