首页 > 代码库 > Some Classical Recursive Functions

Some Classical Recursive Functions

<?xml version="1.0" encoding="utf-8"?> Some Classical Recursive Functions <style type="text/css"> </style> <body>

Some Classical Recursive Functions

Table of Contents

  • 1. Find the maximum element of an array recursively.

In this post, I will list the commonly met recursive functions that can light the road for further investigation into the magic world of computer science.
I am not an excellent programmer myself, but through delight practice, the little but essential part of the RECURSIVE PROBLEMS can be understood. So I will present some examples to illustrate what I mean.

1 Find the maximum element of an array recursively.

#define max(a,b)     (a) > (b) ? (a) : (b)

int f_max(int *a,int start,int end)
{
    if (start == end) {
        return a[start];
    }
    return  max(a[start],f_max(a,start + 1,end));
}

void test_max(void)
{
    int a[] = {
        12,1,22,56,4
    };
    printf("max=%d\n",f_max(a,0,4));
}

Author: wujing

Created: 2014-07-13 日 10:45

Emacs 24.3.1 (Org mode 8.2.6)

Validate