河内塔显示输出。如何显示第一个递归调用的 1 个选项卡、第二个递归调用的 2 个选项卡等?
我的任务是添加一堆打印语句来显示河内塔的完整输出,以查看和了解它在幕后所做的事情,而不是仅仅给您最终结果。
class TowersApp {
static int nDisks = 3;
public static void main(String[] args) {
doTowers(nDisks, 'A', 'B', 'C');
}
public static void doTowers(int topN, char from, char inter, char to) {
int i = 0;
if(topN==1) {
System.out.println("Enter (" + topN + " disk): " + "s=" + from + ", i=" + inter + ", d=" + to);
System.out.println("Base case: move disk " + topN + " from " + from + " to "+ to);
System.out.println("Return (" + topN + " disk)"); }
else {
System.out.println("Enter (" + topN + " disks): " + "s=" + from + ", i=" + inter + ", d=" + to);
doTowers(topN-1, from, to, inter);
System.out.println("Move bottom disk " + topN +
" from " + from + " to "+ to);
doTowers(topN-1, inter, from, to);
System.out.println("Return (" + topN + " disks)");
}
}
}
这是我的输出。我唯一缺少的是缩进。我需要有 1 个选项卡用于第一级递归,2 个选项卡用于第二级递归,依此类推...这就是我的意思:
当前输出:
Enter (3 disks): s=A, i=B, d=C
Enter (2 disks): s=A, i=C, d=B
Enter (1 disk): s=A, i=B, d=C
Base case: move disk 1 from A to C
Return (1 disk)
Move bottom disk 2 from A to B
Enter (1 disk): s=C, i=A, d=B
Base case: move disk 1 from C to B
Return (1 disk)
Return (2 disks)
Move bottom disk 3 from A to C
Enter (2 disks): s=B, i=A, d=C
Enter (1 disk): s=B, i=C, d=A
Base case: move disk 1 from B to A
Return (1 disk)
Move bottom disk 2 from B to C
Enter (1 disk): s=A, i=B, d=C
Base case: move disk 1 from A to C
Return (1 disk)
Return (2 disks)
Return (3 disks)
所需输出:
Enter (3 disks): s=A, i=B, d=C
Enter (2 disks): s=A, i=C, d=B
Enter (1 disk): s=A, i=B, d=C
Base case: move disk 1 from A to C
Return (1 disk)
Move bottom disk 2 from A to B
Enter (1 disk): s=C, i=A, d=B
...................................
我需要某种计数器来“计数”多少个我进入该功能的次数?但是,这是否可以通过递归实现呢?也许我分析过度了,什么时候有一个更简单的解决方案来解决这个问题?
My task was to add a bunch of print statements to display the the complete output of Tower of Hanoi to see and understand what it is doing behind the scenes, instead of just giving you the final result.
class TowersApp {
static int nDisks = 3;
public static void main(String[] args) {
doTowers(nDisks, 'A', 'B', 'C');
}
public static void doTowers(int topN, char from, char inter, char to) {
int i = 0;
if(topN==1) {
System.out.println("Enter (" + topN + " disk): " + "s=" + from + ", i=" + inter + ", d=" + to);
System.out.println("Base case: move disk " + topN + " from " + from + " to "+ to);
System.out.println("Return (" + topN + " disk)"); }
else {
System.out.println("Enter (" + topN + " disks): " + "s=" + from + ", i=" + inter + ", d=" + to);
doTowers(topN-1, from, to, inter);
System.out.println("Move bottom disk " + topN +
" from " + from + " to "+ to);
doTowers(topN-1, inter, from, to);
System.out.println("Return (" + topN + " disks)");
}
}
}
Here's what I have as my output. The only thing I am missing is indentation. I need to have 1 tab for the first level of recursion, 2 tabs for second level of recursion and so on... Here's what I mean:
Current Output:
Enter (3 disks): s=A, i=B, d=C
Enter (2 disks): s=A, i=C, d=B
Enter (1 disk): s=A, i=B, d=C
Base case: move disk 1 from A to C
Return (1 disk)
Move bottom disk 2 from A to B
Enter (1 disk): s=C, i=A, d=B
Base case: move disk 1 from C to B
Return (1 disk)
Return (2 disks)
Move bottom disk 3 from A to C
Enter (2 disks): s=B, i=A, d=C
Enter (1 disk): s=B, i=C, d=A
Base case: move disk 1 from B to A
Return (1 disk)
Move bottom disk 2 from B to C
Enter (1 disk): s=A, i=B, d=C
Base case: move disk 1 from A to C
Return (1 disk)
Return (2 disks)
Return (3 disks)
Desired Output:
Enter (3 disks): s=A, i=B, d=C
Enter (2 disks): s=A, i=C, d=B
Enter (1 disk): s=A, i=B, d=C
Base case: move disk 1 from A to C
Return (1 disk)
Move bottom disk 2 from A to B
Enter (1 disk): s=C, i=A, d=B
...................................
Would I need some sort of counter to "count" how many times I've gone into the function? But then, is that even possible with recursion? Perhaps I am over analyzing when there's a much simpler solution to this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单(尽管可能不太优雅)的方法是将递归深度作为函数的完整参数传递,并随着每个新的深度级别递增它:
从
0
开始,作为主函数中的深度。然后,只需为每个深度
打印一个\t
。The simplest, although probably inelegant, way is to just pass the recursive depth as an integral argument to your function and increment it with every new level of depth:
Start out with
0
for depth in your main function. Then, just print a\t
for everydepth
.一种简单的方法是向 doTowers 添加一个名为
indent
的字符串参数,该参数将添加到所有输出的前面。来自 main 的调用将传递一个空字符串,doTowers 中的每个调用都会向该参数添加空格或制表符(即doTowers(indent+" ",...)
)。您可以将每个System.out.println(...)
更改为System.out.println(indent+...)
A simple way is to add a String parameter named
indent
to doTowers that would be prepended to all the output. The call from main would pass an empty string, each call within doTowers would add spaces or tabs to that parameter (i.e.doTowers(indent+" ",...)
). You'd change eachSystem.out.println(...)
toSystem.out.println(indent+...)
当然,计数器可以很好地解决这个问题。你如何沿着递归得到它?很简单,作为参数传递!
Sure, a counter solves this nicely. How do you get it along the recursion? Simple, pass it as a parameter!
为什么不传入一个随每次递归调用而递增的附加参数呢?
稍后
深度将显示要使用的选项卡数量。第一次调用的深度为 0。
Why not pass in an additional parameter which you increment with each recursive call?
and later on
The depth will show you how many tabs to use. First call will be with a 0 depth.