1. 程式人生 > >java版資料結構與演算法—遞迴(漢若塔)

java版資料結構與演算法—遞迴(漢若塔)

package com.zoujc.triangle;

/**
 * 漢諾塔
 */
class TowersApp {
    public static void main(String[] args){
        doTowers(3,'A','B','C');
    }
    public static void doTowers(int topN,char from,char inner,char to){
        if(topN == 1){
            System.out.println("Disk 1 from " + from + " to "
+ to); }else { doTowers(topN-1,from,to,inner); System.out.println("Disk " + topN + " from " + from + " to " + to); doTowers(topN-1,inner,from,to); } } }

在這裡插入圖片描述