#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
const int N=20;
struct MM
{
int m_max;
int m_min;
};
MM maxmin(int *a,int low,int high)
{
MM mm,t;
if(high==low+1)
{
mm.m_max=(a[high]>a[low])?a[high]:a[low];
mm.m_min=(a[high]<=a[low])?a[high]:a[low];
}
else if(high==low)
mm.m_max=mm.m_min=a[low];
else
{
mm=maxmin(a,low,low+(high-low)/2);
t=maxmin(a,low+(high-low)/2+1,high);
mm.m_max=(mm.m_max>t.m_max)?mm.m_max:t.m_max;
mm.m_min=(mm.m_min<t.m_min)?mm.m_min:t.m_min;
}
return mm;
}
int main()
{
int a[N];
MM mm;
srand((unsigned)time(NULL));
for(int i=0;i<N;++i)
{
a[i]=rand()%100;
cout<<a[i]<<' ';
}
cout<<endl;
mm=maxmin(a,0,N-1);
cout<<"max: "<<mm.m_max<<endl<<"min: "<<mm.m_min<<endl;
cout << "Hello world!" << endl;
return 0;
}
分治算法_求数组中最大最小整数
最新推荐文章于 2022-12-21 15:15:22 发布