Loading...

Във форума е въведено ограничение, което позволява на потребителите единствено да разглеждат публикуваните въпроси.

KremenaNikolova avatar KremenaNikolova 0 Точки

10. Crossroads

Здравейте, може ли някой да ми помогнеда открия къде греша?

линк към моето решение: https://pastebin.com/dNkMEHd6

линк към judge: https://judge.softuni.org/Contests/Submissions/View/28886554

Получавам 71 от 100 точки.

Благодаря предварително!

1.*Crossroads

Our favorite super-spy action hero Sam is back from his mission from the previous exam and he has finally found some time to go on a holiday. He is taking his wife somewhere nice and they're going to have a really good time, but first, they have to get there. Even on his holiday trip, Sam is still going to run into some problems and the first one is, of course, getting to the airport. Right now, he is stuck in a traffic jam at a very active crossroad where a lot of accidents happen.

Your job is to keep track of traffic at the crossroads and report whether a crash happened or everyone passed the crossroads safely and our hero is one step closer to a much-desired vacation.

The road Sam is on has a single lane where cars queue up until the light goes green. When it does, they start passing one by one during the green light and the free window before the intersecting road's light goes green. During one second only one part of a car (a single character) passes the crossroads. If a car is still at the crossroads when the free window ends, it will get hit at the first character that is still in the crossroads.

Input

  • On the first line, you will receive the duration of the green light in seconds – an integer in the range [1…100].
  • On the second line, you will receive the duration of the free window in seconds – an integer in the range [0…100].
  • On the following lines, until you receive the "END" command, you will receive one of two things:
    • A car – a string containing any ASCII character, or
    • The command "green" indicates the start of a green light cycle

A green light cycle goes as follows:

  • During the green light, cars will enter and exit the crossroads one by one.
  • During the free window, cars will only exit the crossroads.

Output

  • If a crash happens, end the program and print:
    "A crash happened!"
    "{car} was hit at {characterHit}."
  • If everything goes smoothly and you receive an "END" command, print:
    "Everyone is safe.".
    "{totalCarsPassed} total cars passed the crossroads.".

Constraints

  • The input will be within the constraints specified above and will always be valid. There is no need to check it explicitly.

Examples

Input

Output

Comments

10

5

Mercedes

green

Mercedes

BMW

Skoda

green

END

Everyone is safe.

3 total cars passed the crossroads.

During the first green light (10 seconds), the Mercedes (8) passes safely.

During the second green light, the Mercedes (8) passes safely and there are 2 seconds left.

The BMW enters the crossroads and when the green light ends, it still has 1 part inside ('W') but has 5 seconds to leave and passes successfully.

The Skoda never enters the crossroads, so 3 cars passed successfully.

9

3

Mercedes

Hummer

green

Hummer

Mercedes

green

END

A crash happened!

Hummer was hit at e.

Mercedes (8) passes successfully and Hummer (6) enters the crossroads but only the 'H' passes during the green light. There are 3 seconds of a free window, so "umm" passes and the Hummer gets hit at 'e' and the program ends with a crash.

 

Тагове:
0
C# Advanced
Axiomatik avatar Axiomatik 2422 Точки

100% Code

using System;
using System.Linq;
using System.Collections.Generic;

namespace crossroads
{
    class Program
    {
        static void Main(string[] args)
        {
            int greenLight = int.Parse(Console.ReadLine());
            int window = int.Parse(Console.ReadLine());
            Queue<string> cars = new Queue<string>();
            int carCounter = 0;
            bool carCrash = false;

            string command = Console.ReadLine();

            while (command.ToUpper() != "END")
            {
                if (command.ToLower() == "green")
                {
                    int currentLight = greenLight;

                    string currentCar = string.Empty;

                    while (currentLight > 0)
                    {
                        if (cars.Count > 0)
                        {
                            currentCar = cars.Dequeue();

                            if (currentLight - currentCar.Length < 0)
                            {
                                if (currentLight - currentCar.Length + window >= 0)
                                {
                                    carCounter++;
                                    break;
                                }
                                else
                                {
                                    char hitCharacter = currentCar[currentLight + window];
                                    Console.WriteLine("A crash happened!");
                                    Console.WriteLine($"{currentCar} was hit at {hitCharacter}.");
                                    carCrash = true;
                                    break;

                                }
                            }

                            currentLight -= currentCar.Length;
                            carCounter++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    cars.Enqueue(command);
                }

                if (carCrash)
                {
                    break;
                }

                command = Console.ReadLine();
            }

            if (carCrash == false)
            {
                Console.WriteLine("Everyone is safe.");
                Console.WriteLine($"{carCounter} total cars passed the crossroads.");
            }
        }
    }
}

;-)

0
KremenaNikolova avatar KremenaNikolova 0 Точки

Axiomatik, благодаря ти за кода, но аз работещи кодове мога да си намеря, искам да знам при моят къде е грешката

0
mira23a avatar mira23a 2 Точки

The examples you give are very easy to understand, I enjoyed your uno online lesson

-1
Можем ли да използваме бисквитки?
Ние използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Можете да се съгласите с всички или част от тях.
Назад
Функционални
Използваме бисквитки и подобни технологии, за да предоставим нашите услуги. Използваме „сесийни“ бисквитки, за да Ви идентифицираме временно. Те се пазят само по време на активната употреба на услугите ни. След излизане от приложението, затваряне на браузъра или мобилното устройство, данните се трият. Използваме бисквитки, за да предоставим опцията „Запомни Ме“, която Ви позволява да използвате нашите услуги без да предоставяте потребителско име и парола. Допълнително е възможно да използваме бисквитки за да съхраняваме различни малки настройки, като избор на езика, позиции на менюта и персонализирано съдържание. Използваме бисквитки и за измерване на маркетинговите ни усилия.
Рекламни
Използваме бисквитки, за да измерваме маркетинг ефективността ни, броене на посещения, както и за проследяването дали дадено електронно писмо е било отворено.