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

[More exercises] Technologies Fundamentals C# - Dictionaries, Lambda and LINQ - Problem{1} - Ranking

Здравейте колеги,

Имам нужда от помощ. Не мога да разбера къде гърми последния тест на решението ми?

https://pastebin.com/LqA1urL8

Условието на задачата:

https://softuni.bg/downloads/svn/soft-tech/Sept-2018/CSharp/06-Dictionaries/More%20Exercise/06.%20Dictionaries-More-Exercises.docx

0
Programming Fundamentals 03/12/2018 22:23:49
Mkbalashkov avatar Mkbalashkov 23 Точки
Best Answer

Не минава, защото може да имаш няколко юзара с еднакъв максимален брой точки. Трябва да го провериш :)

Dictionary<string, int> usersTootalPoints = new Dictionary<string, int>();
            foreach (var kvp in result)
            {
                usersTootalPoints[kvp.Key] = kvp.Value.Values.Sum();
            }

            int maxPoints = usersTootalPoints
                .Values
                .Max();

            foreach (var kvp in usersTootalPoints)
            {
                if (kvp.Value == maxPoints)
                {
                    Console.WriteLine($"Best candidate is {kvp.Key} with total {kvp.Value} points.");

                }
            }

1
Galin.P87 avatar Galin.P87 44 Точки

Прави си, благодаря! Заблудих се, защото в условите към Input е записано:

  • There will be no case with 2 or more users with same total points!
0
Ljubo6 avatar Ljubo6 21 Точки

И аз се възползвах от съвета на колегата.Ето и моето решение:

https://pastebin.com/NejsvvLu

0
kalojan avatar kalojan 9 Точки
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace Sets_and_Dictioanries
{
    class Program
    {
        static void Main(string[] args)
        {
            var passwordsForContests = new Dictionary<string, string>();
            var contests = new SortedDictionary<string, Dictionary<string, int>>();
            var totalPoints = new Dictionary<string, int>();

            var bestPoints = int.MinValue;
            var bestUser = string.Empty;

            while (true)
            {
                var input = Console.ReadLine();
                if (input == "end of contests")
                {
                    break;
                }
                var inputParts = input.Split(":");

                var contest = inputParts[0];
                var password = inputParts[1];

                if (!contests.ContainsKey(contest))
                {
                    passwordsForContests[contest] = password;
                }

            }

            while (true)
            {
                var input = Console.ReadLine();
                if (input == "end of submissions")
                {
                    break;
                }
                var inputParts = input.Split("=>");
                var contest = inputParts[0];
                var password = inputParts[1];
                var username = inputParts[2];
                int points = int.Parse(inputParts[3]);


                if (!passwordsForContests.ContainsKey(contest) || !passwordsForContests.ContainsValue(password))
                {
                    continue;
                }
                else
                {
                    if (!contests.ContainsKey(username))
                    {
                        contests[username] = new Dictionary<string, int>();
                        contests[username].Add(contest, points);
                        totalPoints.Add(username, points);

                    }
                    else
                    {
                        if (contests[username].ContainsKey(contest))
                        {
                            if (contests[username][contest] < points)
                            {
                            contests[username][contest] = points;
                            totalPoints[username] += points;
                            }
                        }
                        else if (!contests[username].ContainsKey(contest))
                        {
                            contests[username].Add(contest, points);
                            totalPoints[username] += points;
                        }
                    }
                }
            }

            foreach (var points in totalPoints)
            {
                if (bestPoints < points.Value)
                {
                    bestPoints = points.Value;
                    bestUser = points.Key;
                }
            }

            Console.WriteLine($"Best candidate is {bestUser} with total {bestPoints} points.\nRanking:");

            foreach (var user in contests)
            {
                Console.WriteLine(user.Key);
                foreach (var contest in user.Value.OrderByDescending(x=>x.Value))
                {
                    Console.WriteLine($"#  {contest.Key} -> {contest.Value}");
                }
            }

        }
    }
}

Цял ден си блъсках главата и все 60/100. Малко помощ?

0