function HanNuoTower(N,a,b,c)
if nargin == 0
N = 3;
a = 'a';
b = 'b';
c = 'c';
end
if (N == 1)
disp(['[',num2str(N),']','----from----',a,'------->',c]);
else
HanNuoTower(N-1,a,c,b);
disp(['[',num2str(N),']','----from----',a,'------->',c]);
HanNuoTower(N-1,b,a,c);
end
end
C#实现
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace HanNuo
{classProgram{staticvoidMain(string[] args){HanNuo(3,'a','b','c');}staticvoidHanNuo(int n ,char a ,char b,char c){if(n ==1){
Console.WriteLine("[{0}]----from----{1}----->{2}",n,a,c);}else{HanNuo(n -1, a, c, b);
Console.WriteLine("[{0}]----from----{1}----->{2}", n, a, c);HanNuo(n -1, b, a, c);}}}}```