分治算法-最大值

 1 #include<iostream>
 2 using namespace std;
 3 
 4 int maxnum(int a[],int low,int high)
 5 {
 6     if(low==high)
 7         return a[low];
 8     else
 9     {
10         int mid=(low+high)/2;
11         int m1=maxnum(a,low,mid);
12         int m2=maxnum(a,mid+1,high);
13         return m1>m2?m1:m2;
14     }
15 }
16 int main()
17 {
18     int a[10]={3,5,7,1,2,9,8,11,10,4};
19     cout<<maxnum(a,0,9)<<endl;
20     return 0;
21 }

 

posted @ 2016-12-13 16:42  自在飞花轻似梦  阅读(221)  评论(0编辑  收藏  举报