如何编写非递归程序? [英] How to write a nonrecursive program ?

查看:57
本文介绍了如何编写非递归程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

河内之塔是一个着名的问题,关于将一定数量的磁盘从一个挂钩移动到另一个挂钩。我为它写了一个递归程序。如果

你已经知道了,你可以跳过这个程序。

#include< stdio.h>

void hanoi(int num,char initial,char terminal,char temporary);

void main(){

int num; / * num是初始挂钩的磁盘数* /

char a =' 'a'',b =''b'',c =''c'';

/ * a是初始挂钩,c是终端挂钩,b用作临时挂头< br $> b $ b peg * /

printf(输入磁盘数量:);

scanf("%d",&) num);

hanoi(num,a,c,b);

}

void hanoi(int num,char initial,char terminal ,char临时){

if(num == 1)

printf("%c ==>%c\ n",initial,terminal);

else {/ *以相反的方式移动磁盘* /

hanoi(num-1,初始,临时,终端);

printf("%c ==>%c \ n",initial,terminal);

hanoi(num-1,临时,终端,首字母);

}

}

我可以轻松编写一个带递归函数的程序。但很多书

说任何可以编程的程序递归实现可以迭代实现
。我不知道如何迭代地编写相应的

。你能告诉我如何编写一个非递归的程序吗? />
与递归程序具有相同的功能

我的书中有许多关于递归程序的练习。对于大多数它们的b $ b,我发现它是很难写出相应的迭代

程序。你能告诉我我需要阅读什么样的书吗?

我很感谢你的帮助!

The Towers of Hanoi is a famous problem about moving a certain number
of disks from one peg to another.I write a recursive program for it.If
you have known it ,you can skip the program.
#include <stdio.h>
void hanoi(int num,char initial,char terminal,char temporary);
void main (){
int num;/*num is the number of disks of the initial peg*/
char a=''a'',b=''b'',c=''c'';
/*a is the initial peg ,c is the terminal peg,b is used as a temporary
peg*/
printf("enter the number of disks:");
scanf("%d",&num);
hanoi(num,a,c,b);
}
void hanoi(int num,char initial,char terminal,char temporary){
if (num==1)
printf("%c==>%c\n",initial,terminal);
else{/*move the disks in a recrusive way*/
hanoi(num-1,initial,temporary,terminal);
printf("%c==>%c\n",initial,terminal);
hanoi(num-1,temporary,terminal,initial);
}
}
I can write a program with a recursive funcation easily.But many books
say that any program that can be implemented recursively can be
implemented iteratively.I don''t know how to write a corresponding
iteratively.Could you tell me how to write a nonrecursive program that
have the same funcation with a recursive program
There are many exercises in my book about recursive programs.For most
of them,I find it is hard to write a corresponding iterative
program.Could you tell me what kind of books I need to read?
I am grateful for your help!

推荐答案

11月14日,6:17 * pm,960392 ... @ qq.com写道:
On Nov 14, 6:17*pm, 960392...@qq.com wrote:

河内的塔楼是关于移动某个数字的着名问题从一个挂钩到另一个挂钩的
磁盘。我为它写了一个递归程序。如果你知道它,你可以跳过程序。

#include< stdio.h>

void hanoi(int num,char initial,char terminal,char temporary);

void main(){
The Towers of Hanoi is a famous problem about moving a certain number
of disks from one peg to another.I write a recursive program for it.If
you have known it ,you can skip the program.
#include <stdio.h>
void hanoi(int num,char initial,char terminal,char temporary);
void main (){



来自C-FAQ:

1.25b:什么是main()的正确声明?

无效main()是否正确?


答:见问题11.12a到11.15。 (但不,它不正确。)

From the C-FAQ:
1.25b: What''s the right declaration for main()?
Is void main() correct?

A: See questions 11.12a through 11.15. (But no, it''s not correct.)


* * * * int num; / * num是初始磁盘的数量peg * /

* * * * char a =''a'',b =''b'',c =''c'';

/ * a是最初的挂钩,c是终端挂钩,b用作临时挂牌

挂钩* /

* * * * printf("输入磁盘数量) :");

* * * * scanf("%d",& num);

* * * * hanoi(num,a,c, b);}


void hanoi(int num,char initial,char terminal,char temporary){

* * * * if(num == 1 )

* * * * * * * * printf("%c ==>%c\ n,初始,终端);

* * * * else {/ *以相反的方式移动磁盘* /

* * * * * * * * hanoi(num-1,初始,临时,终端);

* * * * * * * * printf("%c ==>%c \ n",initial,terminal);

* * * * * * * * hanoi(num -1,临时,终端,初始);

* * * *}}


我c很容易写一个带递归函数的程序。但是很多书都说b / b $ b $表示可以递归实现的任何程序都可以实现迭代实现。我不知道怎么做迭代地写一个相应的

。你能告诉我如何写一个非递归的程序,

与递归程序有相同的功能

那里在我的书中有很多关于递归程序的练习。对于大多数它们来说,我发现很难写出相应的迭代

程序。你能告诉我什么样的我需要阅读的书吗?
* * * * int num;/*num is the number of disks of the initial peg*/
* * * * char a=''a'',b=''b'',c=''c'';
/*a is the initial peg ,c is the terminal peg,b is used as a temporary
peg*/
* * * * printf("enter the number of disks:");
* * * * scanf("%d",&num);
* * * * hanoi(num,a,c,b);}

void hanoi(int num,char initial,char terminal,char temporary){
* * * * if (num==1)
* * * * * * * * printf("%c==>%c\n",initial,terminal);
* * * * else{/*move the disks in a recrusive way*/
* * * * * * * * hanoi(num-1,initial,temporary,terminal);
* * * * * * * * printf("%c==>%c\n",initial,terminal);
* * * * * * * * hanoi(num-1,temporary,terminal,initial);
* * * * }}

I can write a program with a recursive funcation easily.But many books
say that any program that can be implemented recursively can be
implemented iteratively.I don''t know how to write a corresponding
iteratively.Could you tell me how to write a nonrecursive program that
have the same funcation with a recursive program
There are many exercises in my book about recursive programs.For most
of them,I find it is hard to write a corresponding iterative
program.Could you tell me what kind of books I need to read?



您可以通过管理自己的堆栈将迭代转换为递归。我很久以前就用
做了一个非递归的快速排序。


如果它只是尾递归,不要担心它,因为

编译器可能会将它重写为你的迭代。


你可以直接编程,或写一个抽象的堆栈并推送和

pop项目。

You can convert iteration to recursion by managing your own stack. I
did a non-recursive quicksort that way a long time ago.

If it is only tail recursion, don''t worry about it because the
compiler will probably rewrite it as iterative for you anyway.

You can program it directly, or write an abstract stack and push and
pop items.


< 96 ******* @ qq.comwrote in message

news:d7 ** ******************************** @ v16g2000 prc.googlegroups.com ...
<96*******@qq.comwrote in message
news:d7**********************************@v16g2000 prc.googlegroups.com...

河内之塔是一个着名的问题,即将一定数量的磁盘从一个挂钩移动到另一个挂钩。我为它写了一个递归程序。如果

你知道它,你可以跳过这个程序。
The Towers of Hanoi is a famous problem about moving a certain number
of disks from one peg to another.I write a recursive program for it.If
you have known it ,you can skip the program.



(剪断)

(snipped)


我可以轻松编写一个带递归函数的程序。但很多书

说任何可递归实现的程序都可以迭代实现
。我不知道如何迭代地编写相应的

。你能告诉我如何编写一个非递归的程序,

与递归程序具有相同的功能

我的书中有许多关于递归程序的练习。对于大多数

他们,我发现很难写出相应的迭代

程序。你能告诉我我需要阅读什么样的书吗?

我很感谢你的帮助!
I can write a program with a recursive funcation easily.But many books
say that any program that can be implemented recursively can be
implemented iteratively.I don''t know how to write a corresponding
iteratively.Could you tell me how to write a nonrecursive program that
have the same funcation with a recursive program
There are many exercises in my book about recursive programs.For most
of them,I find it is hard to write a corresponding iterative
program.Could you tell me what kind of books I need to read?
I am grateful for your help!



在这个特殊情况下,维基百科知道所有:
http://en.wikipedia.org/wiki/Tower_o...rsive_solution

至于其他问题..它经常有助于尝试想出一个自下而上的b $ b算法 - 通常它不再是递归的(但这里没有保证)

你总是可以作弊通过在软件中模拟堆栈,但这是作弊。

In this particular case, wikipedia knows all:
http://en.wikipedia.org/wiki/Tower_o...rsive_solution
As to other problems.. It often helps to try to think of a "bottom-up"
algorithm - often it won''t be recursive anymore (but no guarantees here)
You could always cheat by emulating a stack in software, but it''s cheating.


在2008-11-15, 96 ******* @ qq.com < 96 ******* @ qq.comwrote:
On 2008-11-15, 96*******@qq.com <96*******@qq.comwrote:

我可以轻松编写一个带递归函数的程序。但很多书

说任何可以递归实现的程序都可以实现
迭代地。我不知道如何迭代地写一个相应的

。你能告诉我怎么写一个nonrecu另一个程序,

与递归程序具有相同的功能
I can write a program with a recursive funcation easily.But many books
say that any program that can be implemented recursively can be
implemented iteratively.I don''t know how to write a corresponding
iteratively.Could you tell me how to write a nonrecursive program that
have the same funcation with a recursive program



次要niggle - 你编写的程序在任何情况下都不是递归的/>
(它不会调用自身)。这是函数hanoi()是递归的



-

Andrew Smallshaw
一个***** @ sdf.lonestar.org


这篇关于如何编写非递归程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆