いまさら聞けないアルゴリズム その1「ハノイの塔」
なんの変哲もないC#で書いた「ハノイの塔」です。
それ以上でもそれ以下でもありません。もちろん模範解答というわけでもない。
using System; using System.Collections.Generic; namespace ConsoleApplication1 { public class Program { private static void Main(string[] args) { List<string> result = Hanoi(3, "A", "B", "C"); Console.WriteLine(result.Count + "回移動"); foreach (string s in result){ Console.WriteLine(s); } Console.Read(); } private static List<string> Hanoi(int n, string a, string b, string c) { List<String> result = new List<string>(); Hanoi(n, a, b, c, ref result); return result; } private static void Hanoi(int n, string a, string b, string c, ref List<String> result) { if (n > 0){ Hanoi(n - 1, a, c, b, ref result); result.Add(a + "から" + c + "へ" + n.ToString() + "を移動"); Hanoi(n - 1, b, a, c, ref result); } } } }
稀に再帰も書けないような自称プログラマな人に出会うことがあるんだけど、
正直勘弁してもらいたい。企業が未経験な人を採ること自体は別によいんだけど、
教育をしないのは問題かと。IT業界大丈夫ですかね?きっと大丈夫じゃないね・・。