Професионална програма
Loading...
+ Нов въпрос
petaryankov00 avatar petaryankov00 3 Точки

03.PlantDiscovery

Здравейте,имам проблем с една от задачите от Programming Fundamentals Final Exam - 09 August 2020. Judge ми дава 87/100 и не мога да разбера къде ми е грешката. Ще съм благодарен, ако някой може да помогне. Благодаря:)

Ето моето решение: https://pastebin.com/z6FHWkCV

Условието:

You have now returned from your world tour. On your way you have discovered some new plants and you want to gather some information about them and create an exhibition to see which plant is highest rated.

On the first line you will receive a number n. On the next n lines, you will be given some information about the plants that you have discovered in the format: "{plant}<->{rarity}". Store that information, because you will need it later. If you receive a plant more than once, update its rarity.

After that until you receive the command "Exhibition" you will be given some of these commands:

  • Rate: {plant} - {rating}add the given rating to the plant (store all ratings)
  • Update: {plant} - {new_rarity}update the rarity of the plant with the new one
  • Reset: {plant}remove all the ratings of the given plant

Note: If any of the command is invalid, print "error"

After the command "Exhibition" print the information that you have about the plants in the following format:

Plants for the exhibition:
- {plant_name}; Rarity: {rarity}; Rating: {average_rating formatted to the 2nd digit}

The plants should be sorted by rarity descending, then by average rating descending

Input / Constraints

  • You will recive the input as described above
  • JavaScript: you will receive a list of strings

Output

Print the information about all plants as described above

1
Fundamentals Module
MartinBG avatar MartinBG 3828 Точки
Best Answer

Има два проблема в решението:

  1. Вместо да се изтрият всички рейтинги при команда Reset, те се сетват на 0, което ще обърка изчислението на средния рейтинг, в случай че след тази команда се получи и команда Rate за растението.
    Например, ако сме имали рейтинги { 4, 3 } + Reset + Rate(15), ще имаме { 0, 0, 15 } и среден рейтинг 5, вместо 15.
    Примерно решение на проблема:
                    else if (cmdArgs[0] == "Reset")
                    {
                        if (plants.ContainsKey(name))
                        {
                            if (ratings.ContainsKey(name))
                            {
                                ratings[name].Clear();
                            }
                        }
                        else
                        {
                            Console.WriteLine("error");
                        }
                    }


     

  2. Грешен среден рейтинг (NaN вместо 0) за растения, които нямат рейтинг.
    Променете:
    double average = sum / item.Value.Count;
    На: 
    double average = sum > 0 ? sum / item.Value.Count : 0;


Оправеното решение. 

0