コード例 #1
0
        public static void Main()
        {
            var number = int.Parse(Console.ReadLine());
            var pumps  = new Queue <GasPump>();

            for (int i = 0; i < number; i++)
            {
                var line = Console.ReadLine()
                           .Split()
                           .Select(int.Parse)
                           .ToArray();

                var     amountOfGas    = line[0];
                var     distanceToNext = line[1];
                GasPump pump           = new GasPump(distanceToNext, amountOfGas, i);
                pumps.Enqueue(pump);
            }
            GasPump startPump       = null;
            var     completeJourney = false;

            while (true)
            {
                GasPump currentPump = pumps.Dequeue();
                pumps.Enqueue(currentPump);

                startPump = currentPump;
                int gasInTank = currentPump.amountOfGas;

                while (gasInTank >= currentPump.distanceToNext)
                {
                    gasInTank  -= currentPump.distanceToNext;
                    currentPump = pumps.Dequeue();
                    pumps.Enqueue(currentPump);
                    if (currentPump == startPump)
                    {
                        completeJourney = true;
                        break;
                    }

                    gasInTank += currentPump.amountOfGas;
                }
                if (completeJourney)
                {
                    Console.WriteLine(currentPump.index);
                    break;
                }
            }
        }
コード例 #2
0
ファイル: Startup.cs プロジェクト: krasiIvanov/SoftUni
        private static void Main(string[] args)
        {
            int             num   = int.Parse(Console.ReadLine());
            Queue <GasPump> pumps = new Queue <GasPump>();

            for (int i = 0; i < num; i++)
            {
                int[]   pumpsInfo = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                GasPump newPump   = new GasPump(pumpsInfo[0], pumpsInfo[1], i);
                pumps.Enqueue(newPump);
            }

            GasPump starterPump = null;
            bool    flag        = false;

            while (true)
            {
                GasPump currentPump = pumps.Dequeue();
                pumps.Enqueue(currentPump);
                starterPump = currentPump;

                int gasInTank = currentPump.amountOfPump;

                while (gasInTank >= currentPump.distanceToNext)
                {
                    gasInTank  -= currentPump.distanceToNext;
                    currentPump = pumps.Dequeue();
                    pumps.Enqueue(currentPump);
                    if (currentPump == starterPump)
                    {
                        flag = true;
                        break;
                    }
                    gasInTank += currentPump.amountOfPump;
                }
                if (flag)
                {
                    Console.WriteLine(starterPump.index);
                    break;
                }
            }
        }
コード例 #3
0
ファイル: TruckTour.cs プロジェクト: Tokotome/AdvancedCSharp
        static void Main(string[] args)
        {
            int             n     = int.Parse(Console.ReadLine());
            Queue <GasPump> pumps = new Queue <GasPump>();

            for (int i = 0; i < n; i++)
            {
                string[] pumpInfo       = Console.ReadLine().Split();
                int      distanceToNext = int.Parse(pumpInfo[1]);
                int      amountOfGas    = int.Parse(pumpInfo[0]);
                GasPump  pump           = new GasPump(distanceToNext, amountOfGas, i);
                pumps.Enqueue(pump);
            }
            GasPump starterPump      = null;
            bool    compeleteJourney = false;

            while (pumps.Count > 0)
            {
                GasPump currentPump = pumps.Dequeue();
                pumps.Enqueue(currentPump);
                starterPump = currentPump;
                int gasInTank = currentPump.amountOfGas;
                while (gasInTank >= currentPump.distanceToNext)
                {
                    gasInTank  -= currentPump.distanceToNext;
                    currentPump = pumps.Dequeue();
                    pumps.Enqueue(currentPump);
                    if (currentPump == starterPump)
                    {
                        compeleteJourney = true;
                        break;
                    }
                    gasInTank += currentPump.amountOfGas;
                }
                if (compeleteJourney)
                {
                    Console.WriteLine(starterPump.indexOfPump);
                    break;
                }
            }
        }