Loading...

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

PhoenixMaster123 avatar PhoenixMaster123 2 Точки

06. Food Shortage

Как може да стане тази задача с Dictionary -> Ако може някакво решение

StartUp:

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

namespace _06._Food_Shortage
{
    public class Program
    {
        static void Main(string[] args)
        {
            string command;         
            Dictionary<string, string> map = new Dictionary<string, string>();
            int count = int.Parse(Console.ReadLine());
            for (int i = 0; i < count; i++)
            {
                string[] names = Console.ReadLine().Split(" ");
                if (names.Length == 4)
                {
                    string name = names[0];
                    int age = int.Parse(names[1]);
                    string id = names[2];
                    string birthday = names[3];

                    ICitizens citizen = new Citizen(name, age, id, birthday);
                    map.Add(name, id);

                }
                else
                {
                    string name = names[0];
                    int age = int.Parse(names[1]);
                    string group = names[2];
                    IRebel rebel = new Rebel(name, age, group);
                    map.Add(name, group);

                }
            }
            int result = 0;
            while ((command = Console.ReadLine()) != "End")
            {              
               foreach(var names in map)
                {
                    if(map.ContainsKey(command) && map.ContainsValue(names.Value))
                    {
                        result += 10;
                        
                    }
                    else if(map.ContainsValue(names.Value))
                    {
                        result += 5;
                        
                    }
                    else
                    {
                       
                    }
                }

            }
            Console.WriteLine(result);

        }
    }
}
 

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

You are not adding the newly created rebels or citizens into your map Dictionary, but just some string-int or string-string pairs, creating these objects is thus superfluous. Would be better to add string-object pair into your Dictionary and then infer the type of the each object with the help of the GetType and typeof operators (which you will definitely need for the exam).

Demo Code with List:

public class Engine3
    {
        public Engine3()
        {
        }

        public void Run()
        {
            int n = int.Parse(Console.ReadLine());
            List<Citizen> citizenList = new List<Citizen>();
            List<Rebel> rebelList = new List<Rebel>();

            AddMembersToLists(n, citizenList, rebelList);

            MembersBuyFood(citizenList, rebelList);

            int totalFood = 0;

            totalFood = TotalFoodCalculator(citizenList, rebelList, totalFood);

            Console.WriteLine(totalFood);
        }

        private static int TotalFoodCalculator(List<Citizen> citizenList, List<Rebel> rebelList, int totalFood)
        {
            foreach (var citizen in citizenList)
            {
                totalFood += citizen.Food;
            }

            foreach (var rebel in rebelList)
            {
                totalFood += rebel.Food;
            }

            return totalFood;
        }

        private static void MembersBuyFood(List<Citizen> citizenList, List<Rebel> rebelList)
        {
            string targetName = Console.ReadLine();

            while (targetName != "End")
            {
                if (citizenList.Any(cit => cit.Name == targetName))
                {
                    citizenList.First(cit => cit.Name == targetName).BuyFood();
                }
                else if (rebelList.Any(reb => reb.Name == targetName))
                {
                    rebelList.First(reb => reb.Name == targetName).BuyFood();
                }

                targetName = Console.ReadLine();
            }
        }

        private static void AddMembersToLists(int n, List<Citizen> citizenList, List<Rebel> rebelList)
        {
            for (int i = 0; i < n; i++)
            {
                string[] personInfo = Console.ReadLine()
                    .Split(" ", StringSplitOptions.RemoveEmptyEntries);

                if (personInfo.Length == 4)
                {
                    string name = personInfo[0];
                    int age = int.Parse(personInfo[1]);
                    string id = personInfo[2];
                    string birthdate = personInfo[3];
                    Citizen currentCitizen = new Citizen(name, age, id, birthdate);
                    citizenList.Add(currentCitizen);
                }
                else if (personInfo.Length == 3)
                {
                    string name = personInfo[0];
                    int age = int.Parse(personInfo[1]);
                    string group = personInfo[2];
                    Rebel currentRebel = new Rebel(name, age, group);
                    rebelList.Add(currentRebel);
                }
            }
        }
    }

 

0
19/07/2022 08:25:09
kristirather avatar kristirather 0 Точки

This solution works for me. Thanks for sharing it. I appreciate it and uno online.

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