Loading...

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

silvana1303 avatar silvana1303 5 Точки

Heart Delivery from Programming Fundamentals Mid Exam - 29 February 2020 Group 2

Успях да докарам 70/100, гледах и други решения на задачата, но с никакви поправки не стигам 100, може ли  някой да каже къде греша? Благодаря предварително!

Кода:https://pastebin.com/cmMBAruT

Условие:

Problem 3. Heart Delivery

Valentine’s Day is coming, and Cupid has very limited time to spread some love across the neighborhood. Help him with his mission!

You will receive a string with even integers, separated by a "@". This is our neighborhood. After that a series of Jump commands will follow, until you receive "Love!" Every house in the neighborhood needs a certain number of hearts delivered by Cupid, in order to be able to celebrate Valentine’s Day. Those needed hearts are indicated by the integers in the neighborhood.

Cupid starts at the position of the first house (index 0) and must jump by a given length. The jump commands will be in this format: "Jump {length}".

Every time he jumps from one house to another, the needed hearts for the visited house are decreased by 2. If the needed hearts for a certain house become equal to 0 , print on the console "Place {houseIndex} has Valentine's day." If Cupid jumps to a house where the needed hearts are already 0, print on the console "Place {houseIndex} already had Valentine's day.".

Keep in mind that Cupid can have a bigger jump length than the size of the neighborhood and if he does jump outside of it, he should start from the first house again.

For example, we are given this neighborhood: 6@6@6. Cupid is at the start and jumps with a length of 2. He will end up at index 2 and decrease the needed hearts there by 2: [6, 6, 4]. Next he jumps again with a length of 2 and goes outside the neighborhood, so he goes back to the first house (index 0) and again decreases the needed hearts there: [4, 6, 4].

Input

  • On the first line you will receive a string with even integers separated by "@" – the neighborhood and the number of hearts for each house.
  • On the next lines, until "Love!" is received, you will be getting jump commands in this format: "Jump {length}".

Output

At the end print Cupid's last position and whether his mission was successful or not:

  • "Cupid's last position was {lastPositionIndex}."
  • If each house has had a Valentine's day, print:
    • "Mission was successful."
  • If not, print the count of all houses that didn`t celebrate a Valentine's Day:
    • "Cupid has failed {houseCount} places."

Constraints

  • The neighborhood`s size will be in the range [1…20]
  • Each house will need an even number of hearts in the range [2 … 10]
  • Each jump length will be an integer in the range [1 … 20]

 

 

Examples

Input

Output

Comments

10@10@10@2

Jump 1

Jump 2

Love!

Place 3 has Valentine's day.

Cupid's last position was 3.

Cupid has failed 3 places.

Jump 1 ->> [10, 8, 10, 2]

 

Jump 2 ->> [10, 8, 10, 0] so we print "Place 3 has Valentine's day."

 

Next command is "Love!", so we print Cupid`s last position and the outcome of his mission.

 

2@4@2

Jump 2

Jump 2

Jump 8

Jump 3

Jump 1

Love!

Place 2 has Valentine's day.

Place 0 has Valentine's day.

Place 0 already had Valentine's day.

Place 0 already had Valentine's day.

Cupid's last position was 1.

Cupid has failed 1 places.

 

 

Тагове:
1
Fundamentals Module
MartinBG avatar MartinBG 4803 Точки
Best Answer

Имаше малко излишен код, като предполагам, че използването на двата индекса Ви е объркало. Дадох им имена според предназначението, с което би следвало да се подобри четимостта на кода.

Това е оправеното решение, като освен проблемите в логиката, съкратих малко и изчисляването на непосетените къщи накрая:

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

namespace exampreparation
{
    class exam
    {
        static void Main(string[] args)
        {
            List<int> houses = Console.ReadLine().Split("@").Select(int.Parse).ToList();

            string[] command = Console.ReadLine().Split().ToArray();

            int lastPosition = 0;

            while (command[0] != "Love!")
            {
                int length = int.Parse(command[1]);

                int currentIndex = lastPosition + length;

                if (currentIndex >= houses.Count)
                {
                    currentIndex = 0;
                }

                if (houses[currentIndex] > 0)
                {
                    houses[currentIndex] -= 2;

                    if (houses[currentIndex] == 0)
                    {
                        Console.WriteLine($"Place {currentIndex} has Valentine's day.");
                    }
                }
                else
                {
                    Console.WriteLine($"Place {currentIndex} already had Valentine's day.");
                }

                lastPosition = currentIndex;
                
                command = Console.ReadLine().Split().ToArray();
            }

            Console.WriteLine($"Cupid's last position was {lastPosition}.");

            if (houses.Sum() == 0)
            {
                Console.WriteLine("Mission was successful.");
            }
            else
            {
                int count = houses.Count(points => points > 0);

                Console.WriteLine($"Cupid has failed {count} places.");
            }
        }
    }
}

 

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