递归
应看到每个步骤的相同点
结束点
特征Characteristics of Recursive Methods
The key features common to all recursive routines:
• It calls itself.
• When it calls itself, it does so to solve a smaller problem.
• There’s some version of the problem that is simple enough that the routine can solve it, and return, without calling itself.
The Towers of Hanoi
public static void doTowers(int topN,
char src, char inter, char dest)
{
if(topN==1)
System.out.println("Disk 1 from " + src + " to "+ dest);
else
{
doTowers(topN-1, src, dest, inter); // src to inter
System.out.println("Disk " + topN + // move bottom
" from " + src + " to "+ dest);
doTowers(topN-1, inter, src, dest); // inter to dest
}
}