gpt4 book ai didi

java - 汉诺塔 - java

转载 作者:太空宇宙 更新时间:2023-11-04 06:25:12 28 4
gpt4 key购买 nike

我有一个用java编写的汉诺塔程序,我能够让它工作,但我一生都无法弄清楚如何让它显示它的运行情况,例如“磁盘1从TowerA到TowerC”,“磁盘2从TowerA到TowerB”等。

这是我的代码:

有没有办法将“磁盘#从(TowerA、TowerB 或 TowerC)添加到(TowerA、TowerB 或 TowerC)”?任何帮助将不胜感激。

最佳答案

试试这个

public class TowerApp
{
static int nDisks = 3;
static public LinkStack A = new LinkStack("A");
static public LinkStack B = new LinkStack("B");
static public LinkStack C = new LinkStack("C");


static public void doTowers(int nDisks, LinkStack source, LinkStack temp, LinkStack dest)
{
if(nDisks <= 4)
if ((nDisks % 2) == 0)
{
displayStacks(source, temp, dest);
nDisks = nDisks - 1;
long dn = source.pop();
temp.push(dn);
System.out.println("Disk # "+dn+" moved from Tower "+source.getName() +" to Tower "+temp.getName());
displayStacks(dest, source, temp);

dn = source.pop();
dest.push(dn);
System.out.println("Disk # "+dn+" moved from Tower "+source.getName() +" to Tower "+dest.getName());

doTowers(nDisks, temp, source, dest);
}
else
{
displayStacks(source, dest, temp);
nDisks = nDisks - 1;
long dn = source.pop();
dest.push(dn);
System.out.println("Disk # "+dn+" moved from Tower "+source.getName() +" to Tower "+dest.getName());
displayStacks(temp, source, dest);
}

/* else if (nDisks >= 5)
{
doTowers(nDisks - 2, source, temp, dest);
temp.push(source.pop());
doTowers(nDisks - 2, dest, source, temp);
dest.push(source.pop());
doTowers(nDisks - 1, temp, source, dest);
}*/
}
static public void displayStacks(LinkStack source, LinkStack temp, LinkStack dest)
{
long n = source.pop();
temp.push(n);
System.out.println("Disk #"+n+" moved from Tower "+source.getName() +" to Tower "+temp.getName());
PrintStacks();
n = source.pop();
dest.push(n);
System.out.println("Disk #"+n+" moved from Tower "+source.getName() +" to Tower "+dest.getName());
PrintStacks();
n= temp.pop();
dest.push(n);
System.out.println("Disk #"+n+" moved from Tower "+temp.getName() +" to Tower "+dest.getName());
PrintStacks();
}

static public void PrintStacks()
{
A.displayStack("TowerA");
B.displayStack("TowerB");
C.displayStack("TowerC");
System.out.println("");
}

public static void main(String[] args)
{
for (int i = nDisks; i >= 1; i--)
{
A.push(i);
}
PrintStacks();

doTowers(nDisks, A, B, C);
}
}

和 LinkStack 类看起来像

class LinkStack
{

private LinkedList theList;
private String name;

public LinkStack(String name) // constructor
{
theList = new LinkedList();
this.name = name;
}
public void push(long j) // put item on top of stack
{
theList.addFirst(j);
}
public long pop() // take item from top of stack
{
return (Long)theList.removeFirst();
}
public boolean isEmpty() // true if stack is empty
{
return ( theList.isEmpty() );
}
public void displayStack(String name)
{
System.out.print(name + ": (top-->bottom): ");
System.out.println(theList);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

输出:

TowerA: (top-->bottom): [1, 2, 3]
TowerB: (top-->bottom): []
TowerC: (top-->bottom): []

Disk #1 moved from Tower A to Tower C
TowerA: (top-->bottom): [2, 3]
TowerB: (top-->bottom): []
TowerC: (top-->bottom): [1]

关于java - 汉诺塔 - java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26882472/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com