Created
April 10, 2012 13:48
-
-
Save lowstz/2351500 to your computer and use it in GitHub Desktop.
河内塔
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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