用递归求数组中的最大值和最小值,这个函数
1个回答
展开全部
#include<stdio.h>
//递归求解对大最小值
void findMinMax(int a[],int l,int r,int *maxnum,int *minnum)
{
if(l==r)
{
*maxnum=*minnum=a[l];
return ;
}
int mid,lmin,rmin,lmax,rmax;
mid = (l+r)/2;
findMinMax(a,l,mid,&lmax,&lmin);
findMinMax(a,mid+1,r,&rmax,&rmin);
*maxnum=lmax>rmax?lmax:rmax;
*minnum=lmin<rmin?lmin:rmin;
}
int main()
{
int a[10]={
1,2,3,4,22,6,7,8,-1,10
},minnum,maxnum;
findMinMax(a,0,9,&maxnum,&minnum);
printf("%d %d",maxnum,minnum);
}
//
//递归求解对大最小值
void findMinMax(int a[],int l,int r,int *maxnum,int *minnum)
{
if(l==r)
{
*maxnum=*minnum=a[l];
return ;
}
int mid,lmin,rmin,lmax,rmax;
mid = (l+r)/2;
findMinMax(a,l,mid,&lmax,&lmin);
findMinMax(a,mid+1,r,&rmax,&rmin);
*maxnum=lmax>rmax?lmax:rmax;
*minnum=lmin<rmin?lmin:rmin;
}
int main()
{
int a[10]={
1,2,3,4,22,6,7,8,-1,10
},minnum,maxnum;
findMinMax(a,0,9,&maxnum,&minnum);
printf("%d %d",maxnum,minnum);
}
//
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询