哎~~
![](/skin/img/sigline.gif)
Go confidently in the directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
哎~~
把那四个判断改成3元运算符的形式会好很多
还有就是上面的判断条件最好改下,具体如下:
p.x=p1.x>p2.x?p1.x:p2.x;
p.y=p1.y<p2.y?p1.y:p2.y;
if(high-low<=1)
下面这样就可以了,你只是数据疏忽了3个地方
#include<iostream>
using namespace std;
struct point //定义一个结构体用来存放x,y
{
int x;
int y;
};
point MinMax(int A[], int low, int high) //寻找最大和最小元素
{
point p,p1,p2;
if(high==low)
{
p.x=p.y=high;
return p;
}
else if((high -low) == 1)
{
if(A[high] > A[low])
{
p.x = A[low];
p.y = A[high];
return p;
}
else
{
p.x = A[high];
p.y = A[low];
return p;
}
}
else
{
int mid = (high + low)/2; //0-1;2-2;3-4;5-6;7-7;8-9
p1 = MinMax(A, low, mid);//递归调用
p2 = MinMax(A, mid+1, high);
if(p1.x >= p2.x)
{
p.x = p2.x;
}
if(p1.x < p2.x)
{
p.x = p1.x;
}
if(p1.y >= p2.y)
{
p.y = p1.y;
}
if(p1.y < p2.y)
{
p.y = p2.y;
}
return p;
}
}int main()
{
point p3;//定义一个point类型的变量,用于接收返回值
int a[10]={1,23,24,1,6,23,34,2,54,6};
p3 = MinMax(a,0,9);
cout<<p3.x<<\" \"<<p3.y<<endl;
return 0;
}