Advertisement

采用递归方法找出数组的最大值

阅读量:

这段代码的编写过程相对直接,因此暂不进行详细说明。如有疑问,欢迎在下方留言交流。由于本人能力有限,若内容存在不足之处,敬请理解与包容。

复制代码
    #include<stdio.h>
    #define max(s,f)(s>f?s:f)
    
    maxnum(int a[], int begin,int c) {   //sizeof在子函数中求不出参数传递的数组大小
    	int lengch = c - begin;		//注意sizeof在子函数中与主函数中求数组长度的区别
    	if (lengch == 1) {
    		return a[begin];
    	}
    	else {
    		return max(a[begin], maxnum(a, begin + 1, c)); 
    		
    	}
    }
    int main() {
    	int a[] = { 1,4,3,10,2 };
    	int c = sizeof(a) / sizeof(a[0]);
    	maxnum(a, 0, c);
    	int m = maxnum(a, 0,c);
    	printf("%d\n", m);
    	return 0;```

全部评论 (0)

还没有任何评论哟~