请各位帮一下忙:给定n个数,在最坏情况下用╔ 3n/2-2 ╕次比较找出这n个数中元素的最大值和最小值。
#include<stdio.h>void maxmin(int a[],int low,int high,int *max,int *min)
{
int mid,hmax,hmin,gmax,gmin;
if(low==high)
{*max=a[low];
*min=a[low];
return;
}
if(low==high-1)
if(a[low]>a[high])
{*max=a[low];
*min=a[high];
return;
}
else
{*max=a[high];
*min=a[low];
return;
}
else
{
mid=(low+high)/2;
maxmin(low,mid,&gmax,&gmin);
maxmin(mid+1,high,&hmax,&hmin);
*max=gmax>hmax? gmax:hmax;
*min=gmin<hmin? gmin:hmin;
}
}
int main()
{
int max,min,k,a[200];
int m;
while(scanf("%d",&k)&&k)
{
for(m=0;m<k;m++)
scanf("%d",&a[m]);
maxmin(a,0,k-1,&max,&min);
printf("%d %d\n",max,min);
}
}
要求:包含多组测试数据。每组测试数据的第一个元素是整数的个数n,接下来是n个整数。0表示结束。 n<=200