Advertisement
nikolayneykov

Untitled

Mar 8th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         int fieldSize = int.Parse(Console.ReadLine());  //размера на полето
  9.         int[] field = new int[fieldSize];               //самото поле
  10.  
  11.         int[] ladybugIndexes = Console.ReadLine()       //индексите на които има калинки
  12.             .Split(' ')
  13.             .Select(int.Parse)
  14.             .Where(x => x >= 0 && x < fieldSize)        //премахваме индексите които са извън размера на полето
  15.             .ToArray();
  16.  
  17.         foreach (var bugIndex in ladybugIndexes)        //обхождаме индексите където ще има калинки
  18.         {
  19.             field[bugIndex] = 1;                        //слагаме калинка
  20.         }
  21.  
  22.  
  23.         string commandInput = string.Empty;
  24.         while ((commandInput = Console.ReadLine()) != "end")
  25.         {
  26.             string[] command = commandInput.Split(' ');        
  27.             int currentIndex = int.Parse(command[0]);           //индекса на който се намираме
  28.             string direction = command[1];                      //посоката на летене
  29.             int flyLength = int.Parse(command[2]);
  30.             flyLength = direction == "right" ? flyLength : -flyLength;  //ако летим надясно + ако летим на ляво -
  31.  
  32.             if (currentIndex < 0 || currentIndex >= fieldSize || field[currentIndex] == 0)  
  33.             {
  34.                 continue;   //ако настоящия индекс е извън рамките на масива или няма калинка на него скипваме
  35.             }
  36.  
  37.             field[currentIndex] = 0;    //калинката е отлетяла слагаме 0 на нейно място
  38.             currentIndex += flyLength;  //първия и полет
  39.  
  40.             while (currentIndex >= 0 && currentIndex < fieldSize)   //проверяваме дали сме в рамките на полето
  41.             {
  42.                 if (field[currentIndex] == 0)           //ако намерим парко място
  43.                 {
  44.                     field[currentIndex] = 1;            //паркираме и прекъсваме
  45.                     break;
  46.                 }
  47.  
  48.                 currentIndex += flyLength;              //ако не сме намерили парко продължаваме да летим
  49.             }
  50.  
  51.         }
  52.  
  53.  
  54.         Console.WriteLine(string.Join(" ", field));     //принтираме
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement