程序是我自己写的,目的是在输入一个数列(数组)后,选出其中最大的元素和最小的元素,程序如下:
#include "stdio.h"
#include "stdlib.h"
#define MAX 100
int max1(int *d1,int *d2)
{
if(*d1>*d2) return d1;
else return d2;
}
int min1(int *e1,int *e2)
{
if(*e1>*e2) return e2;
else return e1;
}
void maxmin(int a[],int *d,int *e,int low,int high)
{
int mid,d1,e1,d2,e2;
if((high-low)<=1)
{
if(a[high]>a[low])
{
d=a[high];
e=a[low];
}
else
{
d=a[low];
e=a[high];
}
}
else
{
mid=(low+high)/2;
maxmin(a[MAX],d1,e1,low,mid);
maxmin(a[MAX],d2,e2,mid+1,high);
d=max1(d1,d2);
e=min1(e1,e2);
}
}
main()
{
int a[MAX],n,i,high,low,*d,*e,x,y;
printf("Please input the length of the Few row!\n");
printf("The n=");
scanf("%d",&n);
printf("\nPlease input the element of the Few row!\n");
for(i=0;i<n;++i)
{
printf("The a[%d]=",i);
scanf("%d",&a[i]);
}
printf("\nThe Few row is:");
for(i=0;i<n;++i)
{
printf("%d ",a[i]);
}
low=0;
high=n-1;
maxmin(a[MAX],d,e,low,high);
printf("\n\nThe max element is %d",*d);
printf("\nThe min element is %d",*e);
getch();
}
我的编译环境是Win-TC,不知道是哪儿有问题,程序结果莫名其妙,希望高手们可以指点一下……谢谢……
[此贴子已经被作者于2007-9-27 15:43:24编辑过]