import java.util.Scanner;
public class test {
static long l=0;
public static void main(String args[]) {
System.out.println("请输入盘子个数:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println("汉诺塔层数为" + n);
System.out.println("移动方案为:" );
hanoi(n, 'a', 'b', 'c');
System.out.println("需要移动次数:"+l);
} static void hanoi(int n, char a, char b, char c) {
if (n > 0) {
hanoi(n - 1, a, c, b);
move(a, b);
hanoi(n - 1, c, b, a);
l++;
}
} static void move(char x, char y) {
System.out.println(x + "->" + y + "\t");
}
}
public class test {
static long l=0;
public static void main(String args[]) {
System.out.println("请输入盘子个数:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println("汉诺塔层数为" + n);
System.out.println("移动方案为:" );
hanoi(n, 'a', 'b', 'c');
System.out.println("需要移动次数:"+l);
} static void hanoi(int n, char a, char b, char c) {
if (n > 0) {
hanoi(n - 1, a, c, b);
move(a, b);
hanoi(n - 1, c, b, a);
l++;
}
} static void move(char x, char y) {
System.out.println(x + "->" + y + "\t");
}
}
解决方案 »
- 一道比竞赛题还竞赛题的题目
- if(true==a) 和 if(a==true) 的区别???
- 如何修改windows的默认字符集为utf-8???
- Java 实现 一个输入输出类 和febonacci数列
- 讨论:大型网站如何建???
- 请教图像处理的一个简单问题,请指教!
- 求Struts2+Spring2.5+Hibernate3.3 的 整合jar 包下载地址
- v=Double.valueOf(br.readLine()).doubleValue();是什么意思!!!!!
- 请问谁有SimpleChart这个组件的使用实例阿?
- 像我这种情况学Java行吗?答者有分!
- 问问你们
- Ldap怎么样获得上级节点dn
import java.util.Scanner;
public class FacTest {
static long l=0;
public static void main(String args[]) {
System.out.println("请输入盘子个数:");
Scanner s=new Scanner(System.in);
int n=s.nextInt();
System.out.println("汉诺塔层数为" + n);
System.out.println("移动方案为:" );
hanoi(n, 'a', 'b', 'c');
System.out.println("需要移动次数:"+l);
}
static void hanoi(int n, char a, char b, char c) {
if (n ==1 )
System.out.println("需要移动次数:"+l);
else
{
hanoi(n - 1, a, c, b);
move(a, b);
hanoi(n - 1, c, b, a);
l++;
}
}static void move(char x, char y) {
System.out.println(x + "->" + y + "\t");
}
}