Advanced  Services C#-Praktikum Back Next Up Home
Rekursive Methoden (Lösung)
// ------------------------------- Aufgabe 1 ------------------------------- \\
public class FibonacciRekursiv
{
   static void MMain(string[] args)
   {
      for(int i=0 ; i<10 ; i++)
         Console.WriteLine("Fibo("+i+") = " + Fibo(i) ) ;
   }

   public static double Fibo(int n)
   {
      if (n<=1) return n;
      if (n>92) return double.PositiveInfinity;

      return Fibo(n-1) + Fibo(n-2) ;
   }
}  // end class


// ------------------------------- Aufgabe 2 ------------------------------- \\
public class Binomial
{
   static void Main(string[] args)
   {
      for(int n=0 ; n<10 ; n++)
      {
         Console.Write(n + " : ") ;
         for(int v=0; v<=n; v++)
            Console.Write(Binkoff(n,v) + "\t" ) ;
         Console.WriteLine() ;
      }
      Console.WriteLine(Binkoff(4,5));
   }

   public static double Binkoff(int n, int v)
   {
      if (v>n) throw new ArithmeticException(n +" >= " + v + " is false") ;
      if (v==0) return 1 ;
      if (v==n) return 1 ;

      return Binkoff(n-1,v) + Binkoff(n-1, v-1);
   }
}  // end class


// ------------------------------- Aufgabe 3 ------------------------------- \\
public class GGT
{
   static void Main(string[] args)
   {
      Console.WriteLine( Ggt(6,8));
      Console.WriteLine("-----------------");
      Console.WriteLine( Ggt(-75,100));
      Console.WriteLine("-----------------");
      Console.WriteLine( Ggt(75,-100));
      Console.WriteLine("-----------------");
      Console.WriteLine( Ggt(-75,-100));
      Console.WriteLine("-----------------");
      //Console.WriteLine( Ggt(0,17));
      Console.WriteLine("-----------------");
      //Console.WriteLine( Ggt(17,0));
   }

   public static int Ggt(int a, int b)
   {
      if(a==0 || b==0) throw new ArithmeticException("Both arguments must not be 0");

      return (a%b==0) ? b : Ggt(b, a%b) ;
   }
}  // end class
Valid XHTML 1.0 Strict top Back Next Up Home