Advertisement
MARINA_GREBENAROVA

Moving Target

Mar 31st, 2023
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _03._Moving_Target
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<int> numbers = Console.ReadLine()
  12.                 .Split(' ', StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToList();
  15.  
  16.             string command = Console.ReadLine();
  17.  
  18.             while (command != "End")
  19.             {
  20.                 List<string> cmd = command.Split(' ', StringSplitOptions.RemoveEmptyEntries).ToList();
  21.  
  22.                 string action = cmd[0];
  23.  
  24.                 if (action == "Shoot")
  25.                 {
  26.                     int  index = int.Parse(cmd[1]);
  27.                     int power = int.Parse(cmd[2]);
  28.  
  29.                     if (index >= 0 && index < numbers.Count)
  30.                     {
  31.                         numbers[index] -= power;
  32.                     if (numbers [index] <= 0)
  33.                     {
  34.                                                                                                                                                             numbers.RemoveAt(index);
  35.                    
  36.                 }
  37.                 }
  38.                }
  39.  
  40.                else  if (action == "Add")
  41.                 {
  42.                     int index = int.Parse(cmd[1]);
  43.                     int value = int.Parse(cmd[2]);
  44.                     if (index >= 0 && index < numbers.Count)
  45.                     {
  46.                         numbers.Insert(index, value);
  47.                     }
  48.                     else
  49.                     {
  50.                         Console.WriteLine("Invalid placement!");
  51.                     }
  52.                    
  53.                    
  54.  
  55.                 }
  56.              
  57.  
  58.  
  59.                 else if (action == "Strike")
  60.                 {
  61.                     int index = int.Parse(cmd[1]);
  62.                     int radius = int.Parse(cmd[2]);
  63.  
  64.                     if (index >=0 && index <= numbers.Count)
  65.                     {
  66.                         if ((index - radius) >=0 && (index + radius) < numbers.Count )
  67.                         {
  68.                             numbers.RemoveRange(index - radius, 2 * radius + 1);
  69.                         }
  70.                         else
  71.                         {
  72.                             Console.WriteLine("Strike missed!");
  73.                         }
  74.                        
  75.                     }
  76.                  
  77.                 }
  78.                
  79.  
  80.  
  81.  
  82.  
  83.  
  84.                 command = Console.ReadLine();
  85.  
  86.             }
  87.             Console.WriteLine(string.Join("|", numbers));
  88.  
  89.  
  90.         }
  91.     }
  92. }
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement