#include<iostream>
#include<time.h>
using namespace std;
void minmax(int a[],int low,int high,int &mmin,int &mmax);
int main()
{
int a[17];//假定是2的幂
int mmin=0,mmax=0;
srand((unsigned int(time(NULL))));//导入时间种子加每次运行结样试下伪随机数种子
for (int i=0;i<17;i++)
{a[i]=rand();cout<<a[i]<<" ";}
minmax(a,0,16,mmin,mmax);
cout<<"min="<<mmin<<" max="<<mmax<<endl;
return 0;
}
void minmax(int a[],int low,int high,int &mmin,int &mmax)
{
if(low==high){mmin=a[low];mmax=a[high];}
else if((high-low)==1)
{
if(a[low]<a[high])
{mmin=a[low];mmax=a[high];}
else
{mmax=a[low];mmin=a[high];}
}
else
{
int x1,x2,y1,y2;
minmax(a,low,(low+high)/2,x1,y1);
minmax(a,(low+high)/2+1,high,x2,y2);
mmin=(x1<x2?x1:x2);
mmax=(y1<y2?y2:y1);
}
}
(算法设计技巧与分析)MinMax
最新推荐文章于 2023-12-28 21:34:13 发布