分治法!
求集合S的最大元素和最小元素的递归分治算法.
谁能用分治法帮俺做做啊??!!
#include <stdio.h>
#define N 10
void maxmin(int s[N], int max, int min);
int main(void)
{
int a[] = {1,12,21,4,57,78,95,43,62,23};
maxmin(a, 0, 0);
return 0;
}
void maxmin(int s[N], int max, int min)
{int s1[N/2],s2[N/2],i,j,max1,max2,min1,min2;
if (N == 2)
{if (s[0] > s[1]) { max=s[0]; min=s[1];}
else { max=s[1]; min=s[0];}
}
else
{
for (i=0; i< N/2; i++)
s1[i] = s[i];
for (j=N/2; j<N; j++)
s2[j] = s[j];
maxmin(s1, max1, min1);
maxmin(s2, max2, max2);
if (max1 > max2) max = max1; else max = max2;
if (min1 > min2) min = min1; else min = min2;
printf("max is :%d\n",max);
printf("min is :%d\n",min);
}
}