# 递归的简单解释

fn = a | fn

1)        fn = a
2)        fn = fn = a
....
n)        fn = fn =...(n - 1 次)= fn = a

#define a          1

int fn(int n)
{
if( n == 0) return a;
return fn(n - 1)
}

n == 0 是递归终止条件, 当它一定能成立时, a 称为吸收子, fn 是递归过程.

void fn ( int n, int sum)
{
if(n == 0) return;

printf("n = %d, sum = %d", n, sum)
fn(n - 1, sum + n);
printf("n = %d, sum = %d", n, sum)
}

A = {n, sum}, {n - 1, sum + n}, {n - 2, sum + n + n - 1}, ... { 0, sum + n*n - 1 - 2 ... - (n - 1)}
a = 空

F(x, y) = 空.

struct A
{
int n;
int sum;
};

#define N = 30; //最大栈深
void fn(int n, int sum)
{
A stack[N];
int i = n;

stack[n].n = n;
stack[n].sum = sum;
printf("n = %d, sum = %d", stack[i].n, stack[i].sum);

while(i)
{
stack[i - 1].sum = stack[i].sum + i;
stack[i - 1].n = i;

i--;
printf("n = %d, sum = %d", stack[i].n, stack[i].sum);
}

while(i   {
printf("n = %d, sum = %d", stack[i].n, stack[i].sum)
i++;
}
}

#include <stdio.h>

void fn ( int n, int sum)
{
if(n == 0) return;

printf("before n = %d, sum = %d/n", n, sum);
fn(n - 1, sum + n);
printf("after n = %d, sum = %d/n", n, sum);
}

struct A
{
int n;
int sum;
};

#define N  30 //最大栈深

void fn2(int n, int sum)
{
A stack[N];
int i = n;

stack[n].n = n;
stack[n].sum = sum;

while(i)
{
stack[i - 1].sum = stack[i].sum + i;
stack[i - 1].n = i - 1;
printf("before n = %d, sum = %d/n", stack[i].n, stack[i].sum);
i--;
}

while(i<n)
{
i++;
printf("after n = %d, sum = %d/n", stack[i].n, stack[i].sum);

}
}

int main()
{
fn(5, 0);
fn2(5, 0);
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：递归的简单解释 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)