文逸首页 小文论坛 文逸博客 精华文章
 首页 | 新闻 | 论坛 | 博客 | 专题 | FTP | 金融 | 微博 | 图库 | MyHome | 搜索 | 登陆 | 注册 | 帮助 | 设为首页  ·在线人数: 488
 §您的位置:文逸首页 > chinesedragon 's home > 【理论研究】专栏 > 递归的简单解释
chinesedragon 的主页网址:http://wonyen.net/home.aspx?id=chinesedragon 给我留言

 § chinesedragon  的【理论研究】专栏

  作者:chinesedragon  发表时间: 2005/12/3 13:45:06    查看: 5977     评论: 5
标题: 递归的简单解释

递归的简单解释 
最简单的递归具有这样的形式

fn = a | fn

它的结果就是 a

计算过程如下,是一个数学归纳法.

递归次数) 表达式
1)        fn = a
2)        fn = fn = a
....
n)        fn = fn =...(n - 1 次)= fn = a

所以fn = a

写成C语言就是:
#define a          1

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

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

当吸收子不含有递归过程(直接或间接)时称为 有限递归. 程序设计中的递归都必须是
有限递归.

不论递归过程如何定义,其关键都产生吸收子.整个递归的过程,实际上就是生成一个
参数数列A = { fn(n=1), fn(n = 2), .... fn(n=n-1)}.
其结果就是以吸收子和参数数列为自变量的一个函数: F(a, A).

上例中, fn(n) 的参数就是n, 参数数列就是A = { n, n - 1, n - 2, ..., 0}
函数F(x, y) = x.所以结果为0.

再举个例子:

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) = 空.

结果只是打印出了A数列

不过这个例子说明了一个问题, F(x,y)实际上作用了两次. 第一次以A顺序, 第二次以A的逆序.

第一次发生在生成A数列过程中,第二次发生在销毁A的过程中.
实际上它说明了递归的栈本质.

就是说所有的递归函数都可以改写成非递归的函数(使用栈).
如上题:

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);
}
 

分享到:


 §评论: 递归的简单解释

  回复者:sskj2009    回复时间:2010/1/15 16:18:20    [第1评]
promised to http://www.bestswissreplicas.com/replica-cartier-watches-swiss.html ;Cartier Replica Watches, post the first white http://www.bestswissreplicas.com/replica-panerai-watches-swiss.html ;Panerai Replica Watches, paper for discussion in http://www.bestswissreplicas.com ;cheap rolex replica watches, the May/June 2003 timeframe, the issues dialogue still http://www.bestwatchesreplica.com ;watches replica, had not yet begun http://www.bestwatchesreplica.com/products/iwc-mechanical-classic-watch-iwc-4-486-watch-replica.html ;cheap IWC Mechanical Classic Watch IWC-4-486 watch replica, when this article went http://www.bestwatchesreplica.com ;watches replica, to print in August. In the meantime, IPC and other affected http://www.cheapuggsaleonline.com/products/ugg-womens-bailey-button.html ;cheap ugg UGG Womens Bailey Button sale, industries will have an http://www.cheapuggsaleonline.com/products/ugg-womens-bailey-button.html ;cheap ugg UGG Womens Bailey Button sale, opportunity to comment on http://www.cheapuggsaleonline.com/products/ugg-womens-bailey-button.html ;cheap ugg UGG Womens Bailey Button sale, EPA's planned ICR submission to OMB. The proposed http://www.ebestwatch.com/categories/concord-replicas.html ;exact replica Concord review, ICR, published July 1, http://www.ebestwatch.com/products/patek-philippe-527.html ;exact Patek Philippe Perpetual Calendar P_PHIL-17-774 imitation, is a follow-up to http://www.ebestwatch.com/products/omega-277.html ;replica  Omega, OMB's temporary 7-month approval of EPA's ICR in March http://www.hot-handbags.com/louisvuitton-wallets-purse-handbags/replica-louisvuitton-wallets-purse-monogram-vernis-handbags/replica-louis-vuitton-wallet-lvgg-14-handbags.html ;Louis Vuitton Wallet LVgg--14, 2003. OMB's temporary approval, http://www.hot-handbags.com ;replica handbags, which expires October 31, http://www.hot-handbags.com ;replica handbags, required EPA to develop changes to the http://www.replicatiffanyjewelry.net ;replica tiffany jewelry, TRI reporting forms, in http://www.replicatiffanyjewelry.net/categories/replica-tiffany-money-clips.html ;replica Tiffany Money Clips jewelry, order to reduce the http://www.replicatiffanyjewelry.net/products/new-replica-gucci-jewelry-gjewelry0831095.html ;New Replica Gucci Jewelry gjewelry0831095, burden on facilities reporting under TRI. While EP


  回复者:腾讯    回复时间:2011/11/20 18:17:14    [第2评]
QQ抽奖

QQ抽奖活动

腾讯周年挖宝行动是真的吗

腾讯公司有抽奖活动吗

QQ中奖是真的吗

腾讯中奖是真的吗


  回复者:王博    回复时间:2012/7/24 22:16:42    [第3评]
要求大家点击一下查看这些不法分子!
星光大道抽奖活动
星光大道官方网站
星光大道中奖查询网
星光光大道中奖是真的吗
QQ信封


3 条评论;  每页显示 15 条评论;   1 / 1               ↑到页首




您未登陆,发表评论时请填写:用户名 密码 注册新用户  
 评论: 递归的简单解释
内容 (8000字以内)
 (CTRL+ENTER提交) 
  关闭窗口  
用户登陆
我要发表文章
搜 索
§chinesedragon 的网志导航
感想随笔(4)
生活休闲(0)
饮食健康(0)
自然妙趣(1)
潮流时尚(0)
游览见闻(0)
情感绿洲(6)
娱乐搞笑(4)
读图时代(3)
影音视听(4)
商业新知(0)
理论研究(6)
时事纵横(13)
社会文化(15)
文学欣赏(3)
教育学习(15)
§chinesedragon 的友情链接
关于文逸 | 小文论坛 | 文逸博客 | 文逸金融 | 精华文章网站地图 | 联系我们 | 隐私保护
 Copyright© WWW.WONYEN.NET 2003 - 2021  闽ICP备09016518号-16   本站最高 10508 人同时在线,发生时间 2005-5-17 5:09:15 
 文逸科技 制作维护