Професионална програма
Loading...
+ Нов въпрос
Vondai avatar Vondai 25 Точки
Best Answer

На изхода "You won! The enemy ship has sunken." липсва точката. После дава 100/100.

Поздрави!

0
radostin.georgiev avatar radostin.georgiev 8 Точки

Здравей!

Ето моето решение, но е на PHP. Може и да ти помогне.

https://pastebin.com/Vt8MhG0P

0
kkaraivanov avatar kkaraivanov 427 Точки

Здравей. Ето и едно C# решение

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

namespace ManOWar
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> statusPirateShip = Console.ReadLine()
                .Split('>')
                .Select(int.Parse)
                .ToList();
            List<int> statusWarShip = Console.ReadLine()
                .Split('>')
                .Select(int.Parse)
                .ToList();
            int healthCapacity = int.Parse(Console.ReadLine());
            string commands = null;

            while ((commands = Console.ReadLine()) != "Retire")
            {
                switch (commands.Split()[0])
                {
                    // Fire {index} {damage} 
                    case "Fire":
                        int.TryParse(commands.Split()[1], out int fireIndex);
                        int.TryParse(commands.Split()[2], out int fireDamage);

                        if (!IndexIsValid(statusWarShip, fireIndex))
                            continue;
                        if(HealtIsNotEnough(statusWarShip, fireIndex, fireDamage))
                        {
                            Console.WriteLine("You won! The enemy ship has sunken.");
                            return;
                        }

                        statusWarShip[fireIndex] = statusWarShip[fireIndex] - fireDamage;
                        break;
                    // Defend {startIndex} {endIndex} {damage}
                    case "Defend":
                        int.TryParse(commands.Split()[1], out int startIndex);
                        int.TryParse(commands.Split()[2], out int endIndex);
                        int.TryParse(commands.Split()[3], out int defendDamage);

                        if (!IndexIsValid(statusPirateShip, startIndex) || !IndexIsValid(statusPirateShip, endIndex))
                            continue;
                        if (HealtIsNotEnough(statusPirateShip, startIndex, endIndex, defendDamage))
                        {
                            Console.WriteLine("You lost! The pirate ship has sunken.");
                            return;
                        }

                        for (int i = startIndex; i <= endIndex; i++)
                        {
                            statusPirateShip[i] -= defendDamage;
                        }
                        break;
                    // Repair {index} {health} 
                    case "Repair":
                        int.TryParse(commands.Split()[1], out int repareIndex);
                        int.TryParse(commands.Split()[2], out int repareHealt);
                        if (!IndexIsValid(statusPirateShip, repareIndex))
                            continue;

                        statusPirateShip[repareIndex] = Math.Min(healthCapacity, statusPirateShip[repareIndex] + repareHealt);
                        break;
                    case "Status":
                        int sectionNeedRepair = (int)(healthCapacity * 0.20);
                        Console.WriteLine($"{statusPirateShip.Where(x => x < sectionNeedRepair).Count()} sections need repair.");
                        break;
                }
            }

            Console.WriteLine($"Pirate ship status: {statusPirateShip.Sum()}");
            Console.WriteLine($"Warship status: {statusWarShip.Sum()}");
        }

        private static bool HealtIsNotEnough(List<int> arr, int index, int damage)
        {
            return arr[index] - damage <= 0;
        }

        private static bool HealtIsNotEnough(List<int> arr, int startIndex, int endIndex, int damage)
        {
            for (int i = startIndex; i <= endIndex; i++)
            {
                if (arr[i] - damage <= 0)
                    return true;
            }

            return false;
        }

        private static bool IndexIsValid(List<int> arr, int index)
        {
            return index >= 0 && index < arr.Count;
        }
    }
}

 

0
Milko123 avatar Milko123 14 Точки

Благодаря за отговорите. Не можах да оправя грешката си, пробвах всичко :(

0
Milko123 avatar Milko123 14 Точки

Но поне разбрах, че мога да чета PHP код :)

0