java处理汉诺塔问题
2008-05-09 15:26
225 查看
import java.util.Scanner;
public class HanoiTest {
public HanoiTest(){
}
public static void move(String from,String to){
System.out.println("move the top plate from "+from+" to "+to);
}
public static void execute(int n,String from,String temp,String to){
if(n==1){
move(from,to);
}else{
execute(n-1,from,to,temp);
move(from,to);
execute(n-1,temp,from,to);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=null;
try{
sc = new Scanner(System.in);
System.out.print("请输入汉诺塔上盘子的初始数目:");
while(sc.hasNext()){
String s=sc.nextLine();
if(!s.matches("//d+")){
System.out.println("输入错误,请输入数字!");
}
int n=Integer.parseInt(s);
execute(n,"A","B","C");
System.out.print("请输入汉诺塔上盘子的初始数目:");
}
}catch(Exception e){
e.printStackTrace();
}finally{
sc.close();
System.out.println("End!");
}
}
}
public class HanoiTest {
public HanoiTest(){
}
public static void move(String from,String to){
System.out.println("move the top plate from "+from+" to "+to);
}
public static void execute(int n,String from,String temp,String to){
if(n==1){
move(from,to);
}else{
execute(n-1,from,to,temp);
move(from,to);
execute(n-1,temp,from,to);
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=null;
try{
sc = new Scanner(System.in);
System.out.print("请输入汉诺塔上盘子的初始数目:");
while(sc.hasNext()){
String s=sc.nextLine();
if(!s.matches("//d+")){
System.out.println("输入错误,请输入数字!");
}
int n=Integer.parseInt(s);
execute(n,"A","B","C");
System.out.print("请输入汉诺塔上盘子的初始数目:");
}
}catch(Exception e){
e.printStackTrace();
}finally{
sc.close();
System.out.println("End!");
}
}
}
相关文章推荐
- 处理Java编码遇到的一个问题
- java 基础问题的一些处理,记录一下,持续更新呗
- 内存分析方法和JVM的java heap space问题处理
- 10.fabric-java-sdk使用联接池后长时间,报UNAVAILABLE问题处理
- Java导出Excel表(poi)名中文乱码问题处理
- java中处理获取路径有空格(%20 )的问题
- Flex+Java组合处理某些日期(如公元1111年)的问题
- java乱码问题处理
- 汉诺塔问题的java递归实现
- net.sf.json在处理json对象转换为普通java实体对象时的问题和解决方案
- android4.4 Jni函数查找失败问题处理(java.lang.UnsatisfiedLinkError: Native method not found)
- java处理高并发高负载类网站问题
- java处理字符编码的常见问题(最近在弄字符串设置编码写入文件)
- java--第十周--任务一 封装一类对象,功能是随机产生一个2000年后的年份,并输出该年2月的日历页,需处理闰年的问题
- 在解压gz文件时,如果直接用java.util.zip.GZIPInputStream来处理问题只能解压很少一部分内容,通过类MultiMemberGZIPInputStream 可以完全解压一个gz文件。
- Java处理OA中常用的word文档,PDf,excel等问题---2
- 汉诺塔问题的java算法实现
- Java线程(八):锁对象Lock-同步问题更完美的处理方式
- java处理图片时找到不sun.awt.X11GraphicsEnvironment问题
- java报表软件中本地效果与服务器效果不一致问题处理经验分享