GeorgiG05 1 Точки

0.3 P!rates - 05. Programming Fundamentals Final Exam

```https://pastebin.com/WAEhBksC
```

На примерните инпути всичко е вярно, обаче ми дава 58 / 100.

Тагове:
1
krum_43 683 Точки

Здрвей GeorgiG05,

Моето решение също забива на 58/100.

Ще се радвам ако някой сподели решение 100/100 на въпросната задача.

Ето го и моят код (C#):

https://pastebin.com/YcL15AwV

0
22/07/2021 12:53:57
Axiomatik 1626 Точки

;-)

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

namespace pirates
{
class Program
{
static void Main(string[] args)
{
var townList = new Dictionary<string, List<int>>();

while (townInput != "Sail")
{
string[] townSplit = townInput
.Split("||", StringSplitOptions.RemoveEmptyEntries);
if (townSplit.Length == 3)
{
string town = townSplit[0];
int population = int.Parse(townSplit[1]);
int gold = int.Parse(townSplit[2]);
if (!townList.ContainsKey(town))
{
townList[town] = new List<int>();
}
else
{
townList[town][0] += population;
townList[town][1] += gold;
}

}

}

while (commands != "End")
{
string[] cmdSplit = commands
.Split("=>", StringSplitOptions.RemoveEmptyEntries);

string operation = cmdSplit[0];

if (operation == "Plunder")
{
string town = cmdSplit[1];
int population = int.Parse(cmdSplit[2]);
int gold = int.Parse(cmdSplit[3]);
if (townList.ContainsKey(town))
{
Console.WriteLine(\$"{town} plundered! {gold} gold stolen, {population} citizens killed.");
townList[town][0] -= population;
townList[town][1] -= gold;
if (townList[town][0] <= 0 || townList[town][1] <= 0)
{
Console.WriteLine(\$"{town} has been wiped off the map!");
townList.Remove(town);
}
}

}
else if (operation == "Prosper")
{
string town = cmdSplit[1];
int gold = int.Parse(cmdSplit[2]);
if (gold < 0)
{
Console.WriteLine("Gold added cannot be a negative number!");
}
else
{
townList[town][1] += gold;
int currentGold = townList[town][1];
Console.WriteLine(\$"{gold} gold added to the city treasury. {town} now has {currentGold} gold.");
}
}

}

if (townList.Any())
{
Console.WriteLine(\$"Ahoy, Captain! There are {townList.Count} wealthy settlements to go to:");
foreach (var (town, data) in townList.OrderByDescending(data => data.Value[1]).ThenBy(name => name.Key))
{
Console.WriteLine(\$"{town} -> Population: {data[0]} citizens, Gold: {data[1]} kg");
}
}
else
{
Console.WriteLine("Ahoy, Captain! All targets have been plundered and destroyed!");
}

}
}
}``````

1
krum_43 683 Точки

Мерси за кода.

Както обикновено решението ти е отлично.Варианта с един речник е  по-добър,a при мен липсваше проверка за златото(if gold<=0).

Thank you so much !

1
22/07/2021 15:50:11