import java.util.*;
class towers{
public static void main(String args[])
{
honoi('A','B','C',(long)1,n);
}
static int disk_no(long i,int n) // to find the next disk to move
{
if(i%2!=0)
return 1;
else
{
int j;
long k;
for(j=2;j<=n;j++)
for(k=0;k<Math.pow(2,n-j);k++)
if(i==(long)Math.pow(2,(long)j-1)*((2*k)+1))
{
System.out.println("returning :"+j);
return j;
}
return 0;
}
}
static void honoi(char x,char y,char z,long i,int n)
{
if(i<Math.pow(2,n))
{
switch((int)i%2)
{
case 0:System.out.println("STEP "+i+" :\tMove disk "+(long)disk_no(i,n)+" from pole "+z+" to "+y);
honoi(x,y,z,++i,n);break;
default:System.out.println("STEP "+i+" :\tMove disk "+(long)disk_no(i,n)+" from pole "+x+" to "+y);
honoi(y,z,x,++i,n);
}
}
}}
我從用戶讀取n值作爲磁盤數量ofcourse我跳過在這裏 我認爲這個問題是 disk_no() 功能,如果不提在代碼中的邏輯錯誤如果有的話我寫了這段代碼解決了河內的塔。經過32453次迭代我的代碼停止工作
究竟是什麼問題? – Seelenvirtuose
嘗試詳細解釋問題。例如,在32453次迭代之後,局部變量的值是什麼?等等.. – OhadM