c语言 递归拆分数字_C程序使用递归在数字数组中找到最大的数字

c语言 递归拆分数字

Problem statement: Write a C Program to find the Biggest Number in an Array of integers (can be negative too) using Recursion.

问题陈述:编写一个C程序,以使用Recursion在整数数组中查找最大数(也可以为负数)

Algorithm:

算法:

1. Construct a max function which will return maximum of two.
    Function max(a, b)
        return a>b? a: b; //using ternary operator
    End Function

2. Construct recursive functionfindBigRec (array, end index)
    Function findBigRec (array, end index)
        a.  Base case:
                IF end index==0
                    return INT_MIN;

        b.  Return maximum between the last element (array [end index-1]) & 
            maximum of rest of the array. Maximum of rest of the array 
            is calculated recursively.
            return max (array[end index-1], findBigRec (array, end index-1));


Explanation with example:

举例说明:

Let the length of input array: 6
Array elements: 4 12 5 13 7 9
In the main function:

Call findBigRec (array,6);
---------------------------------------------------
findBigRec (array,6):
end index, 6 != 0
return max( array[5], findBigRec (array,5));

Call findBigRec (array,5);
---------------------------------------------------
findBigRec (array,5):
end index, 5 != 0
return max( array[4], findBigRec (array,4));

Call findBigRec (array,4);
---------------------------------------------------
findBigRec (array,4):
end index, 4 != 0
return max( array[3], findBigRec (array,3));

Call findBigRec (array,3);
---------------------------------------------------
findBigRec (array,3):
end index, 3 != 0
return max( array[2], findBigRec (array,2));

Call findBigRec (array,2);
---------------------------------------------------
findBigRec (array,2):
end index, 2 != 0
return max( array[1], findBigRec (array,1));

Call findBigRec (array,1);
---------------------------------------------------
findBigRec (array,1):
end index, 1 != 0
return max( array[0], findBigRec (array,0));

Call findBigRec (array,0);
---------------------------------------------------
findBigRec (array,0):
end index, 0 == 0
return INT_MIN;
---------------------------------------------------

findBigRec (array,1) returns max ( array[0], findBigRec (array,0))
				=max (4, INT_MIN)
				=4
---------------------------------------------------
findBigRec (array,2) returns max ( array[1], findBigRec (array,1))
				=max (12, 4)
				=12
---------------------------------------------------
findBigRec (array,3) returns max ( array[2], findBigRec (array,2))
				=max (5, 12)
				=12
---------------------------------------------------
findBigRec (array,4) returns max ( array[3], findBigRec (array,3))
				=max (13, 12)
				=13
---------------------------------------------------
findBigRec (array,5) returns max ( array[4], findBigRec (array,4))
				=max (7, 13)
				=13
---------------------------------------------------
findBigRec (array,6) returns max ( array[5], findBigRec (array,5))
				=max (9, 13)
				=13
Thus max returned by main function is 13
Biggest no in the array is 13


C实现使用递归在数字数组中找到最大的数字 (C implementation to find the Biggest Number in an Array of Numbers using Recursion)

#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

//finding maximum of two element
int max(int a,int b){
	return (a>b)?a:b;
}

int findBigRec(int* a,int n){
	//base case
	if(n==0)
		//can't return 0, since there may be 
		//negative numbers in the array
		return INT_MIN; 
	//recursively process
	//return maximum between the last element 
	//& maximum of rest of array
	//maximum of rest of array is again going 
	//to be recursively processed
	return max(a[n-1],findBigRec(a,n-1));
}

int main()
{   
	int n;
	
	printf("Enter array length: ");
	scanf("%d",&n);
	
	int* a=(int*)(malloc(sizeof(int)*n));
	printf("enter elements...\n");
	//input array elements
	for(int i=0;i<n;i++)
		scanf("%d",&a[i]);
	
	//recursive function to find the maximum of the array
	int big=findBigRec(a,n);

	printf("The biggest element in the array is: %d\n",big);

	return 0;
}

Output

输出量

Enter array length: 6
enter elements...
4 12 5 13 7 9
The biggest element in the array is: 13 


翻译自: https://www.includehelp.com/c-programs/find-the-biggest-number-in-an-array-of-numbers-using-recursion.aspx

c语言 递归拆分数字

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值