河内塔 Java 递归

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10328038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:35:24  来源:igfitidea点击:

Towers Of Hanoi Java Recursion

javarecursion

提问by salxander

Good evening everyone,

大家晚上好,

I have a quick question on a homework assignment my class is doing about recursion. The idea is that we have this towers of hanoi program, and we need to write a main that will make a table that will display the numbers 5-25, and how many moves it would take to solve a tower of that size, for example

我有一个关于我的班级正在做的关于递归的家庭作业的快速问题。这个想法是我们有这个河内塔程序,我们需要编写一个 main 来制作一个表格,显示数字 5-25,以及解决这个大小的塔需要多少步,例如

5 ---- 31 Moves

5 ---- 31 移动

6 ---- 63 Moves

6 ---- 63 移动

etc...

等等...

I am having a little bit of trouble doing that as the TowersOfHanoi class is set up to print out each move, and I don't think we're supposed to get rid of that, but I'm not too sure.

我在这样做时遇到了一些麻烦,因为 TowersOfHanoi 类设置为打印出每个移动,我认为我们不应该摆脱它,但我不太确定。

Here is the TowersOfHanoi class

这是 TowersOfHanoi 类

public class TowersOfHanoi {
    private int totalDisks;
    private int count;

    public TowersOfHanoi(int disks) {
        totalDisks = disks;
        count = 0;
    }

    public void solve() {
        moveTower (totalDisks,1,3,2);
    }

    private void moveTower(int numDisks, int start, int end, int temp) {
        if (numDisks ==1) {
            moveOneDisk(start,end);
        }
        else {
            moveTower (numDisks-1, start, temp, end);
            moveOneDisk (start, end);
            moveTower (numDisks-1, temp, end, start);
        }
    }

    private void moveOneDisk(int start, int end) {
        count = count+1;
        System.out.println("Move one disk from "+start+" to "+end+" - Move "+count);
    }
}

Now I just need to write a main that will create that table without printing out every single move for every single tower, but I'm not really sure how to. Any help is much appreciated

现在我只需要编写一个 main 来创建该表,而无需打印出每个塔的每一个动作,但我不确定如何去做。任何帮助深表感谢

采纳答案by codaddict

You'll need one object of TowersOfHanoiclass for each disk you'll be solving the puzzle for. For this you'll create these objects in the mainmethod by passing them different arguments (from 5 to 25). Once the object is constructed you just call the solvemethod on it.

您将需要TowersOfHanoi为每个要解决难题的磁盘创建一个类对象。为此,您将main通过向它们传递不同的参数(从 5 到 25)在方法中创建这些对象。构造对象后,您只需调用其solve上的方法即可。

I'll leave the implementation to you as this is tagged homework.

我将把实现留给你,因为这被标记为家庭作业。

回答by Itchy Nekotorych

you could remove the print statement in moveOneDisk, then after you call the recursion method in main write a print statement that will output count.

您可以删除 moveOneDisk 中的打印语句,然后在调用 main 中的递归方法后编写一个将输出计数的打印语句。

private void moveOneDisk(int start, int end) {
    count = count+1;
    //System.out.println("Move one disk from "+start+" to "+end+" - Move "+count);
}

public static void main(){
TowersOfHanoi tower = TowersOfHanoi(5);
tower.solve();
system.out.print(tower.count);
}//end of main

removing the print statement in the moveOneDisk would make it so that not every single move is reported, but the count will still increment. then you can assign whatever variable needed when constructing the table with the class member count. in this case tower.count

删除 moveOneDisk 中的打印语句将使它不会报告每一个移动,但计数仍然会增加。然后您可以在使用类成员计数构造表时分配所需的任何变量。在这种情况下,tower.count

回答by Mahendar Reddy

package midterm;

包期中;

public class TowersofHanoi {
    public int totalDisks;
    public static int count;

    public TowersofHanoi(int disks) {
        totalDisks = disks;
        count = 0;
    }

    public void solve() {
        moveTower (totalDisks,1,3,2);
    }

    private void moveTower(int numDisks, int start, int end, int temp) {
        if (numDisks ==1) {
            moveOneDisk(start,end);


        }
        else {
            moveTower (numDisks-1, start, temp, end);

            moveOneDisk (start, end);
            moveTower (numDisks-1, temp, end, start);
        }
    }

    private void moveOneDisk(int start, int end) {
        count = count+1;
        //System.out.println("Move one disk from "+start+" to "+end+" - Move "+count);
    }
    public static void main(){
        TowersofHanoi tower = new TowersofHanoi(5);
        tower.solve();

        System.out.println(tower.count);
        }

}