/************************************************ Grundlegende Algorithmen mit Java, http://algorithmen-und-problemloesungen.de/ Copyright @2007-2009 by Doina Logofatu in C#: Michael Gärtner ************************************************/ using System; using System.IO; namespace Logofatu { class P02Hanoi { private static String FileOutputName = "hanoi.out"; private StreamWriter sw; public P02Hanoi(StreamWriter sw) { this.sw = sw; } private void write(char a, char b) { sw.Write('('); sw.Write(a); sw.Write(','); sw.Write(b); sw.Write(')'); } public void hanoi(int n, char a, char c, char b) { if ( 1 == n ) write(a, c); else { hanoi(n - 1, a, b, c); write(a, c); hanoi(n - 1, b, c, a); } } static void Main(string[] args) { StreamWriter sw = new StreamWriter(FileOutputName); try { Console.Write(" n = "); int n = int.Parse(Console.ReadLine()); new P02Hanoi(sw).hanoi(n, 'A', 'C', 'B'); } catch ( IOException ex ) { Console.WriteLine("Fehler bei der Dateiverarbeitung!\n" + ex); } finally { sw.Close(); } } } }