用递归方法设计函数int Arraymin(int d[ ],int size);

来源:百度知道 编辑:UC知道 时间:2024/05/25 00:51:31
各位大虾帮帮忙。。。其中d为数组,size为数组元素个数,ArrayMin返回d中的最小元素

#include <stdio.h>

int Arraymin(int d[],int size)
{
if ( size == 1 )
{
return d[0];
}
else
{

int t;
if ( d[0]< d[1] )
{
t=d[0];
d[0]=d[1];
d[1]=t;
}
return Arraymin( d+1, size-1);
}
}
int main()
{

int d[] = { 10, 35, 6, 989, 1 };
printf( "%d", Arraymin( d, 5 ) );
return 0;
}

int Arraymin(int d[], int size)
{
int min;
if (size == 1) {
return d[0];
}
min = Arraymin(&d[1], size - 1));
if (d[0] > min)
return min;
else
return d[0];
}

#include <iostream.h>

int Arraymin(int d[],int size)
{
if(size>0)
{
int result = Arraymin(d+1, size-1);
return (d[0]<result)?d[0]:result;
} else {
return d[size-1];
}
}
<