Skip to content

Instantly share code, notes, and snippets.

@lowstz
Created April 10, 2012 13:48
Show Gist options
  • Save lowstz/2351500 to your computer and use it in GitHub Desktop.
Save lowstz/2351500 to your computer and use it in GitHub Desktop.
河内塔
//: HanoiTwoer.java
public class HanoiTower {
// 教材用递归,我还是用循环把 =。=
public static void move(int n, char from, char to, char aux) {
if(n == 1) {
System.out.println("将#1盘从" + from + "移到" + to);
}
else {
for(int i=1; i<n; i++){
System.out.println("将#"+ i + "盘从" + from + "移到" + aux);
}
System.out.println("将#" + n + "盘从"+ from +"移到" + to);
while(n != 1) {
System.out.println("将#"+ (n-1) + "盘从" + aux +"移到"+ to);
n--;
}
}
}
public static void main(String[] args) {
move(10, 'A','C','B');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment