Untitled

 avatar
unknown
plain_text
2 years ago
1.3 kB
4
Indexable
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zadanie3
{
    class Program
    {
        static void WiezaHanoi(int n) //n - liczba krazkow
        {
            Stack<int> i = new Stack<int>();
            Stack<int> j = new Stack<int>();
            Stack<int> k = new Stack<int>();
            //i - slupek startowy, k - pomocniczy, j - docelowy
            while (n > 0)
            {
                if (n % 2 != 0)
                {
                    i.Push(j.Pop());
                    n--;
                }
                if (n % 2 == 0)
                {
                    i.Push(k.Pop());
                    k.Push(i.Pop());
                    i.Push(j.Pop());
                    n--;
                }
            }
            while (j.Count > 0)
                Console.Write(j.Pop());
            Console.WriteLine();
        }
            static void Main(string[] args)
        { /* Wykorzystując strukturę stos napisz metodę rozwiązującą problem wież
             Hanoi bez rekurencji. */
             Console.WriteLine("Podaj liczbę krążków: ");
             int n = Convert.ToInt32(Console.ReadLine());
             WiezaHanoi(n);

             Console.ReadKey();
        }
    }
}
Editor is loading...