#include <stdio.h>
#include <stdlib.h>
#define N 10
int min( int a, int b){
return a < b ? a : b;
}
int minRecur(int *a, int start, int end){
if( start == end ){
return a[ start ];
}
return min(a[start], minRecur( a, start + 1, end ));
}
int minRecur2(int *a ,int n){
if(n == 1){
return a[0];
}
return min(a[0], minRecur2(&a[1], n - 1));
}
void printA(int *a, int n){
int i;
for(i = 0; i < n; i++){
printf("[%d]:%d ", i, a[i]);
}
printf("\n");
}
int main(){
int i, a[N] = {0};
srand(time(NULL));
printA(a, N);
for(i = 0; i < N; i++){
a[i] = rand() % 100;
}
printA(a, N);
printf("minRecur:%d\n",minRecur(a, 0, N - 1 ));
printf("minRecur2:%d\n",minRecur2(a, N ));
return 0;
}
[C How To Program] 习题6.39 递归求数组的最小值
最新推荐文章于 2017-12-08 20:19:38 发布