日期:2014-05-20 浏览次数:20691 次
public class towersApp {
static int nDisks = 3;
public static void main(String[] args) {
doTowers(nDisks, 'A', 'B', 'C');
}
public static void doTowers(int topN, char from, char inter, char to)
{
if(topN ==1)
System.out.println("Disk 1 from " + from + " to " + to);
else
{
doTowers(topN-1, from, to ,inter);
System.out.println("the topN is " + topN);
System.out.println("from is "+from + " inter is " + inter + " to is " + to);
System.out.println("Disk " + topN + " from " + from + " to " +to);
doTowers(topN-1, inter, from, to);
System.out.println("the topN is " + topN);
}
}
}
void func(int i) {
if (i == 0) {
return;
} else {
printf("This is func %d\n", i);
func(i-1);
printf("This is func %d\n", i);
}
}
// Output
This is func 5
This is func 4
This is func 3
This is func 2
This is func 1
This is func 1
This is func 2
This is func 3
This is func 4
This is func 5