недеља, 1. април 2012.

Zen problem

Budistički sveštenik dobija zadatak od svog učitelja da meditira tačno 45 minuta.
Nema sat, ima samo dva mirišljava štapića, od kojih svaki gori tačno jedan sat, ali različitom brzinom. 
Kako će sveštenik izmeriti tačno 45min meditacije?


Pošto budistički sveštenik zna C#, verovatno je napisao ovakav code:


using System;
using System.Threading;
namespace Puzzle
{
    class Program
    {
        public static Thread T1_l, T1_r, T2_l, T2_r;
       
        static void Main(string[] args)
        {
            ST st1 = new ST(60, 50, 1);
            ST st2 = new ST(60, 50, 2);
            T1_l = new Thread(new ThreadStart(st1.F1));
            T1_r = new Thread(new ThreadStart(st1.F2));
            T2_l = new Thread(new ThreadStart(st2.F1));
            T2_r = new Thread(new ThreadStart(st2.F2));
            st1._e1 += new EventHandler(_EH1l);
            st1._e2 += new EventHandler(_EH1r);
            st1._e0 += new EventHandler(_Fin1);
            st2._e1 += new EventHandler(_EH2l);
            st2._e2 += new EventHandler(_EH2r);
            st2._e0 += new EventHandler(_Fin2);
 
            T1_l.Start();
            T1_r.Start();
            T2_l.Start();
 
            Console.ReadLine();
        }
 
        static void _Fin1(object sender, EventArgs e)
        {
            Console.WriteLine("First finish  " + sender.ToString());
            if (!T2_r.IsAlive)
                T2_r.Start();
        }
        static void _EH1l(object sender, EventArgs e)
        {
            Console.WriteLine("Left  " + sender.ToString());
        }
        static void _EH1r(object sender, EventArgs e)
        {
            Console.WriteLine("Right " + sender.ToString());
        }
        static void _Fin2(object sender, EventArgs e)
        {
            Console.WriteLine("Finish  " + sender.ToString());
        }
        static void _EH2l(object sender, EventArgs e)
        {
            Console.WriteLine("     Left  " + sender.ToString());
        }
        static void _EH2r(object sender, EventArgs e)
        {
            Console.WriteLine("     Right " + sender.ToString());
        }
    }
    public class ST
    {
        public event EventHandler _e1, _e2, _e0;
        private decimal _d = 0;
        private int[] v_s;
        private int[] v_t;
        private int[] v_d;
        int x, y = 0;
        private DateTime dt;
        private int tt = 1000;
        private int a = 0;
        Random sl = new Random();
        public ST(int _t, int _i, int _a)
        {
            v_s = new int[_t];
            v_t = new int[_t];
            v_d = new int[_t];
            Console.Write("Stick " + _a.ToString() + " (");
            for (int i = 0; i < _t; i++)
            {
                v_s[i] = sl.Next(1, _i);
                v_t[i] = _t / 60;
                v_d[i] = v_s[i] * v_t[i];
                if (i != (_t - 1))
                    Console.Write(v_s[i].ToString() + ",");
                else
                    Console.Write(v_s[i].ToString());
 
            }
            Console.WriteLine(")");
            dt = DateTime.Now;
            //   this._d = v_s.Sum();
            foreach (int q in v_s)
                this._d += q;
            y = _t;
            this.a = _a;
 
            //    Console.WriteLine("Stick {0} Duration: {1} Time: {2}", _a, v_d.Sum(),_t);
            int e = 0;
            foreach (int q in v_d)
                e += q;
            Console.WriteLine("Stick {0} Duration: {1} Time: {2}", _a, e, _t);
            Console.WriteLine();
        }
        public void F1()
        {
            string _b = "";
            decimal _tt = 0;
            while (Duration > 0)
            {
                if (Duration <= v_d[x])
                {
                    _tt = Math.Round(((Duration / Convert.ToDecimal(v_d[x])) *
                        Convert.ToDecimal(v_t[x]) / 2), 2) * tt;
 
                    if ((Program.T2_r.IsAlive) && a == 2)
                        Program.T2_r.Abort();
                    if ((Program.T1_r.IsAlive) && a == 1)
                        Program.T1_r.Abort();
                }
                else
                    _tt = v_t[x] * tt;
                Thread.Sleep(Convert.ToInt32(_tt));
                _b = String.Format("Speed: {0:D2} Time: {1:F0} Duration: {2:D2} Remaining: {3:F0} Elapsed: {4:D2}",
                    v_s[x], _tt / tt, v_d[x], Duration - v_d[x], dt.Subtract(DateTime.Now).Duration().Seconds);
 
                _e1((object)_b, new EventArgs());
                Duration = v_d[x];
                x++;
            }
        }
        public void F2()
        {
            string _b = "";
            decimal _tt = 0;
            while (Duration > 0)
            {
                y--;
                if (Duration <= v_d[y])
                {
                    _tt = Math.Round(((Duration / Convert.ToDecimal(v_d[y])) *
                        Convert.ToDecimal(v_t[y]) / 2), 2) * tt;
                    if ((Program.T2_l.IsAlive) && a == 2)
                        Program.T2_l.Abort();
                    if ((Program.T1_l.IsAlive) && a == 1)
                        Program.T1_l.Abort();
                }
                else
                    _tt = v_t[y] * tt;
                Thread.Sleep(Convert.ToInt32(_tt));
                _b = String.Format("Speed: {0:D2} Time: {1:F0} Duration: {2:D2} Remaining: {3:F0} Elapsed: {4:D2}",
                 v_s[y], _tt / tt, v_d[y], Duration - v_d[y], dt.Subtract(DateTime.Now).Duration().Seconds);
                _e2((object)_b, new EventArgs());
                Duration = v_d[y];
            }
        }
        private decimal Duration
        {
            get { return _d; }
            set
            {
                _d = _d - value;
                if (_d < 0)
                    _d = 0;
                if (_d == 0)
                {
                   _e0((object)dt.Subtract(DateTime.Now).Duration().Seconds, new EventArgs();
 
                    if ((Program.T1_l.IsAlive) && a == 1)
                        Program.T1_l.Abort();
                    if ((Program.T1_r.IsAlive) && a == 1)
                        Program.T1_r.Abort();
                    if ((Program.T2_l.IsAlive) && a == 2)
                        Program.T2_l.Abort();
                    if ((Program.T2_r.IsAlive) && a == 2)
                        Program.T2_r.Abort();
                }
            }
        }
    }
}


Нема коментара: