ورود

View Full Version : تقسیم و حل



moh.mon
شنبه 01 فروردین 1388, 12:43 عصر
من این کد رو با راهنمایی های دوستان نوشتم ولی نمیدونم مشکلش چیه؟
چون اجرا میشه ولی وسط کار erorr میده.
تو این کد min و max یه آرایه با روش تقسیم و حل پیدا میشه.
اینم سورسش:


#include <iostream>
#include <conio.h>
using namespace std;
void maxmin(int*,int*,int);
void enternumbers(int);
int *a,max1,min1;
int main()
{
int *max,*min,n;
cout<<"\nEnter the number of items:";
cin>>n;

a=new int [n];
if(a==NULL) cout<<"error";
cout<<"\nEnter the numbers:";
enternumbers(n);
maxmin(max,min,n);
cout<<"max= "<<*max<<"\n"<<"min= "<<*min<<endl;
_getch();
return 1;
}
void enternumbers(int n)
{
int i;
for(i=0;i<n;i++)
cin>>a[i];
}
void maxmin(int *max, int *min,int n)
{
if( n == 1)
*min = *max = a[0];
else if( n == 2)
{
if(a[0] < a[1])
{
*min = a[0];
*max = a[1];
}
else
{
*min = a[1];
*max = a[0];
}
}
else
{
maxmin(max,min,n/2);
maxmin(&max1,&min1,n - n/2);
if( min1 < *min)
*min = min1;
if( max1 > *max)
*max = max1;
}
}