汉诺塔已经实现,现在我想在每个盘子移动前输出,"第N步"这样的句子,不过居然不知道加在哪里?指教下用java写的,别介意哈
/**
*
*//**
* @author Administrator
*
*/
public class hannio { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int m; // 汉诺塔盘子数 m = 5;
System.out.println("移动" + m + "个盘子的汉诺塔的步骤:"); hanno(m, 'A', 'B', 'C');
} /**
* 移动函数
*/
private static void move(char x, char y) {
System.out.println("从" + x + "盘移动一个盘子到" + y + "盘");
} /**
* 汉诺递归函数
*/
private static void hanno(int n, char one, char two, char three) {
if (n == 1) {
move(one, three);
} else {
hanno(n - 1, one, three, two);
move(one, three);
hanno(n - 1, two, one, three);
}
}}
/**
*
*//**
* @author Administrator
*
*/
public class hannio { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int m; // 汉诺塔盘子数 m = 5;
System.out.println("移动" + m + "个盘子的汉诺塔的步骤:"); hanno(m, 'A', 'B', 'C');
} /**
* 移动函数
*/
private static void move(char x, char y) {
System.out.println("从" + x + "盘移动一个盘子到" + y + "盘");
} /**
* 汉诺递归函数
*/
private static void hanno(int n, char one, char two, char three) {
if (n == 1) {
move(one, three);
} else {
hanno(n - 1, one, three, two);
move(one, three);
hanno(n - 1, two, one, three);
}
}}
解决方案 »
- 求下高手,winform这个列表控件怎么整
- C# 已经得到窗口句柄 怎么能移动窗口位置?
- 用户权限问题!
- c# 实现文件重一台电脑传输到另一台电脑, 传输的渠道是VPN 或者专线
- 公司现在需要开发一个短网关接口,接口说明已有,但我对整体流程及操作不清楚,求短信网关运作的整体流程??
- 出现了错误提示:这是线程间操作无效: 从不是创建控件“lstPrime”的线程访问它
- 网络游戏公司急招网站开发人员!!!
- asp.net导出Excel 后打不开,没有用npoi组件
- 我急了!(放100分)
- Xamarin for Android 模拟器或真机连接不了解决方案
- C# 操作 excel Workbook_BeforeClose事件里 不让关闭怎么做?
- C#删除IIS站点的问题
* 移动函数
*/
private static void move(char x, char y) {
System.out.println("第" + (++N) + "步:");
System.out.println("从" + x + "盘移动一个盘子到" + y + "盘");
}
*
*//**
* @author Administrator
*
*/
public class hannio { /**
* @param args
*/
static int count=1;
public static void main(String[] args) {
// TODO Auto-generated method stub
int m; // 汉诺塔盘子数 m = 5;
System.out.println("移动" + m + "个盘子的汉诺塔的步骤:"); hanno(m, 'A', 'B', 'C');
} /**
* 移动函数
*/
private static void move( char x, char y) {
System.out.print("第" + (count++) + "步:");
System.out.println("从" + x + "盘移动一个盘子到" + y + "盘");
} /**
* 汉诺递归函数
*/
private static void hanno(int n, char one, char two, char three) {
if (n == 1) {
move( one, three);
} else {
hanno(n - 1, one, three, two);
move(one, three);
hanno(n - 1, two, one, three);
}
}}
*
*//**
* @author Administrator
*
*/
public class hannio { /**
* @param args
*/
int step = 1; // 第Step步
public static void main(String[] args) {
// TODO Auto-generated method stub
int m; // 汉诺塔盘子数 m = 5;
System.out.println("移动" + m + "个盘子的汉诺塔的步骤:"); hanno(m, 'A', 'B', 'C');
} /**
* 移动函数
*/
private static void move(char x, char y) {
System.out.println("第"+(step++)+"从" + x + "盘移动一个盘子到" + y + "盘");
} /**
* 汉诺递归函数
*/
private static void hanno(int n, char one, char two, char three) {
if (n == 1) {
move(one, three);
} else {
hanno(n - 1, one, three, two);
move(one, three);
hanno(n - 1, two, one, three);
}
}}
static int N = 0; public static void Main() {
int m = 5; // 汉诺塔盘子数
System.out.println("移动" + m + "个盘子的汉诺塔的步骤:");
hanno(m, 'A', 'B', 'C');
} private static void move(char x, char y) {
System.out.println("第" + (++N) + "步: 从" + x + "盘移动一个盘子到" + y + "盘");
} private static void hanno(int n, char one, char two, char three) {
if (n == 1) {
move(one, three);
} else {
hanno(n - 1, one, three, two);
move(one, three);
hanno(n - 1, two, one, three);
}
}
}