Advertisement
ntamas

hanoi tornyai

Apr 8th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.79 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace fuggvenyek
  7. {
  8.   class Program
  9.   {
  10.     static int count;
  11.     static void hanoi(int n, char a, char b, char c)
  12.     {
  13.       if (n == 1)
  14.       {
  15.         count++;
  16.         Console.WriteLine("{0} -> {1}", a, c);
  17.       }
  18.       else
  19.       {
  20.         hanoi(n - 1, a, c, b);
  21.         count++;
  22.         Console.WriteLine("{0} -> {1}", a, c);
  23.         hanoi(n - 1, b, a, c);
  24.       }
  25.     }
  26.     static void Main(string[] args)
  27.     {
  28.       Console.Write("Kérem a korongok számát: ");
  29.       int n = Convert.ToInt32(Console.ReadLine());
  30.       if (n > 0)
  31.       {
  32.         hanoi(n, 'A', 'B', 'C');
  33.       }
  34.       Console.WriteLine("Lépések száma: {0}", count);
  35.       Console.ReadKey();
  36.     }
  37.   }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement