/************************************************ Grundlegende Algorithmen mit Java, http://algorithmen-und-problemloesungen.de/ Copyright @2007-2008 by Doina Logofatu in C#: Michael Gärtner ************************************************/ using System; using System.IO; namespace Logofatu { class P08CollatzSeq { private static String FileInputName = "collatzSeq.in"; private static String FileOutputName = "collatzSeq.out"; private int step; private StreamWriter sw; public P08CollatzSeq(StreamWriter sw) { this.step = 0; this.sw = sw; } public void calculate(long n) { sw.Write(n); sw.Write(' '); if ( n != 1 ) { step++; calculate(n % 2 == 0 ? n / 2 : 3 * n + 1); } else { sw.Write("<" + step); sw.Write('>'); sw.WriteLine(); sw.WriteLine(); } } static void Main(string[] args) { StreamReader sr = null; StreamWriter sw = null; try { sr = new StreamReader(FileInputName); sw = new StreamWriter(FileOutputName); while ( !sr.EndOfStream ) { P08CollatzSeq col = new P08CollatzSeq(sw); col.calculate(int.Parse(sr.ReadLine())); } } catch ( IOException ex ) { Console.WriteLine("Fehler bei der Dateiverarbeitung!\n" + ex); } finally { if ( sr != null ) { sr.Close(); } if ( sw != null ) { sw.Close(); } } } } }