国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

二分遞歸查找最大和最小元素

2019-11-17 05:47:26
字體:
供稿:網(wǎng)友
//二分遞歸查找最大和最小元素
//YCL
//2004.10.27
#include<iostream.h>
//定義一個類,表示最大和最小元素的組合
class MaxMin{
PRivate:
 int max,min;
public:
 MaxMin(int x,int y)//構(gòu)造函數(shù),給max和min附最大和最小元素的值
 
        else
  
 }
 ~MaxMin(){}//析構(gòu)函數(shù)
 int GetMax()//獲取其中的最大值
 {
  return max;
 }
 int GetMin()//獲取其中的最小值
 {
  return min;
 }
 void Print()//輸出最大值和最小值
 
};
//從兩個MaxMin類對象中選取最大值和最小值形成新的組合
MaxMin Comp(MaxMin t1,MaxMin t2)
{
 int x,y;
 x=t1.GetMax()>t2.GetMax()?t1.GetMax():t2.GetMax();
 y=t1.GetMin()<t2.GetMin()?t1.GetMin():t2.GetMin();
 MaxMin t(x,y);
 return t;
}
//二分遞歸查找:在數(shù)組a[]的[i,j]范圍內(nèi)查找
MaxMin Bin_MaxMin(int i,int j,int a[])
{
 int mid;
 if(i==ji==j-1)//遞歸出口:當傳入數(shù)組中只有一個或兩個元素時
 {
  MaxMin fmaxfmin(a[i-1],a[j-1]);
  return fmaxfmin;
 }
 else//遞歸調(diào)用,二分查找
 {
  mid=(i+j)/2;
  MaxMin ftemp1=Bin_MaxMin(i,mid,a);
  MaxMin ftemp2=Bin_MaxMin(mid+1,j,a);
  return Comp(ftemp1,ftemp2);
 }
}void main()
{
 int i,j,n;
 const int N=10;
 int a[N];
 do{
  cout<<"How many elements do you want to input?";
  cin>>n;
        if(n<1n>N)
   cout<<"Overflow!Again!"<<endl;
 }while(n<1n>N);//輸入元素個數(shù)
 cout<<"Input the elements:"<<endl;
 for(i=0;i<n;i++)//輸入元素內(nèi)容
  cin>>a[i];
 do{
  cout<<"Input the start point and the end point:";
     cin>>i>>j;
  if((i<1i>n)(j<1j>n)(i>j&&(i>0&&i<n)&&(j>0&&j>n)))
  {
   if(i<1i>n)
    cout<<"The start point overflows!"<<endl;
   if(j<1j>n)
    cout<<"The end point overflows!"<<endl;
   if(i>j&&(i>0&&i<n)&&(j>0&&j>n))
    cout<<"The start point and the end point maybe need to be exchanged!"<<endl;
   cout<<"Input them again!"<<endl;
  }
 }while((i<1i>n)(j<1j>n)(i>j&&(i>0&&i<n)&&(j>0&&j>n)));//輸入查找范圍
 Bin_MaxMin(i,j,a).Print();//輸出查找結(jié)果
}



上一篇:多變的立方體

下一篇:二級指針

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 新竹县| 兴宁市| 河西区| 长阳| 福建省| 方山县| 红桥区| 金溪县| 海林市| 双柏县| 台南市| 禄劝| 新闻| 丰宁| 讷河市| 杂多县| 凌云县| 南汇区| 拜城县| 万全县| 长汀县| 花莲市| 山阳县| 襄樊市| 宝鸡市| 东乡族自治县| 涡阳县| 朔州市| 砚山县| 双牌县| 五原县| 灵武市| 射洪县| 阿鲁科尔沁旗| 从江县| 黎城县| 兴山县| 屏东市| 玉环县| 辉县市| 绥阳县|