/************************************************ Grundlegende Algorithmen mit Java, http://algorithmen-und-problemloesungen.de/ Copyright @2007-2009 by Doina Logofatu in C#: Michael Gärtner ************************************************/ using System; using System.Collections.Generic; using System.IO; namespace Logofatu { class P01GGT { private static String FileInputName = "ggt.in"; private static int gcd(int a, int b) { while ( b != 0 ) { int r = a % b; a = b; b = r; } return a; } private static int divide_et_impera(List a, int iMin, int iMax) { if ( iMin < iMax ) { int middle = (iMin + iMax) / 2; int d1 = divide_et_impera(a, iMin, middle); int d2 = divide_et_impera(a, middle + 1, iMax); return gcd(d1, d2); } return 0 <= iMin && iMin < a.Count ? a[iMin] : 0; } static void Main(string[] args) { List list = new List(); StreamReader sr = null; try { sr = new StreamReader(FileInputName); while ( !sr.EndOfStream ) { string[] sArray = sr.ReadLine().Split(' '); for ( int i = 0; i < sArray.Length; i++ ) { list.Add(int.Parse(sArray[i])); } } int sz = list.Count; if ( sz > 0 ) { Console.WriteLine(" ggT = " + divide_et_impera(list, 0, sz - 1)); } } catch ( IOException ex ) { Console.WriteLine("Fehler bei der Dateiverarbeitung!\n" + ex); } finally { if ( sr != null ) { sr.Close(); } } } } }