Advertisement
ElviraPetkova

ladyBug

Feb 11th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace P10.LadyBugs
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int fieldSize = int.Parse(Console.ReadLine());
  11.  
  12.             int[] lbip = Console.ReadLine()
  13.                 .Split()
  14.                 .Select(int.Parse)
  15.                 .ToArray();
  16.  
  17.             int[] result = new int[fieldSize];
  18.  
  19.             for (int i = 0; i < lbip.Length; i++)
  20.             {
  21.                 if (lbip[i] < 0 || lbip[i] > fieldSize - 1)
  22.                 {
  23.                     continue;
  24.                 }
  25.                 result[lbip[i]] = 1;
  26.             }
  27.  
  28.             string command = Console.ReadLine();
  29.  
  30.             while (command != "end")
  31.             {
  32.                 string[] order = command.Split();
  33.  
  34.                 int position = int.Parse(order[0]);
  35.                 string direction = order[1];
  36.                 int step = int.Parse(order[2]);
  37.  
  38.                 if (!(position >= 0 && position <= result.Length - 1 && result[position] == 1))
  39.                 {
  40.                     command = Console.ReadLine();
  41.                     continue;
  42.                 }
  43.  
  44.                 result[position] = 0;
  45.                 int newPosition = position;
  46.  
  47.                 if (direction == "right")
  48.                 {
  49.                     newPosition += step;
  50.                     try
  51.                     {
  52.                         while (result[newPosition] == 1)
  53.                         {
  54.                             //Мисля че по условие е зададено, че когато на търсения индекс вече има калинка, трябва да продължи
  55.                             //да лети със същата дължина, т.е. newPosition се увеличава със step, а не с единица
  56.                             newPosition += step;
  57.                         }
  58.  
  59.                         result[newPosition] = 1;
  60.                     }
  61.                     catch (Exception)
  62.                     {
  63.                         command = Console.ReadLine();
  64.                         continue;
  65.                     }
  66.                 }
  67.                 else if (direction == "left")
  68.                 {
  69.                     newPosition -= step;
  70.                     try
  71.                     {
  72.                         while (result[newPosition] == 1)
  73.                         {
  74.                             newPosition -= step; //същото като при righ, позицията се намаля със step
  75.                         }
  76.  
  77.                         result[newPosition] = 1;
  78.                     }
  79.                     catch (Exception)
  80.                     {
  81.                         command = Console.ReadLine();
  82.                         continue;
  83.                     }
  84.                 }
  85.  
  86.                 command = Console.ReadLine();
  87.             }
  88.             Console.WriteLine(string.Join(" ", result));
  89.  
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement