你怎么能找到使用递归数组的最小值? [英] How can you find the minimum value in an array using recursion?
本文介绍了你怎么能找到使用递归数组的最小值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须写一个C code,使用递归发现在数组中的最小值。我已经使用了循环已经完成,但递归是棘手。有人能帮助我吗?
I have to write a C code that finds the smallest value in an array using recursion. I've already done it using the for loop, but recursion is trickier . Can someone help me??
推荐答案
下面是简单的code查找使用递归最小值,
Here is simple code for finding minimum value using recursion,
int rec(int a[],int n)
{
int min;
if(n==1)
return a[0];
else {
min=rec(a,n-1);
if(min<a[n-1])
return min;
else
return a[n-1];
}
}
void main()
{
int i,j,n,a[20];
printf("enter n :");
scanf("%d",&n);
printf("enter values : ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n%d",rec(a,n));
getch();
}
这篇关于你怎么能找到使用递归数组的最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文