Advertisement
Guest User

Untitled

a guest
Sep 18th, 2016
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace MatrixOP
  6. {
  7.     class Program
  8.     {
  9.         static List<List<int>> rows = new List<List<int>>();
  10.         static void Main(string[] args)
  11.         {
  12.  
  13.             int rowsCount = int.Parse(Console.ReadLine());
  14.             for (int i = 0; i < rowsCount; i++)
  15.             {
  16.                 rows.Add(Console.ReadLine().Split().Select(int.Parse).ToList());
  17.             }
  18.  
  19.             string input = Console.ReadLine();
  20.             while (input != "end")
  21.             {
  22.                 string[] inputArgs = input.Split();
  23.  
  24.                 switch (inputArgs[0])
  25.                 {
  26.                     case "remove":
  27.                         RemoveCommand(inputArgs);
  28.                         break;
  29.                     case "insert":
  30.                         InsertCommand(inputArgs);
  31.                         break;
  32.                     case "swap":
  33.                         SwapCommand(inputArgs);
  34.                         break;
  35.                 }
  36.                 input = Console.ReadLine();
  37.             }
  38.  
  39.             foreach (List<int> row in rows)
  40.             {
  41.                 Console.WriteLine(string.Join(" ", row));
  42.             }
  43.         }
  44.  
  45.         private static void SwapCommand(string[] input)
  46.         {
  47.             int first = int.Parse(input[1]);
  48.             int second = int.Parse(input[2]);
  49.             List<int> oldFirst = new List<int>(rows[first]);
  50.             List<int> oldSecond = new List<int>(rows[second]);
  51.             rows[first] = oldSecond;
  52.             rows[second] = oldFirst;
  53.         }
  54.  
  55.         private static void InsertCommand(string[] input)
  56.         {
  57.             int row = int.Parse(input[1]);
  58.             int element = int.Parse(input[2]);
  59.             rows[row].Insert(0, element);
  60.         }
  61.  
  62.         private static void RemoveCommand(string[] input)
  63.         {
  64.             bool isRow = input[2] == "row";
  65.             int index = int.Parse(input[3]);
  66.             switch (input[1])
  67.             {
  68.                 case "positive":
  69.                     if (isRow)
  70.                     {
  71.                         rows[index].RemoveAll(p => p >= 0);
  72.                     }
  73.                     else
  74.                     {
  75.                         rows.ForEach(p =
  76.                         {
  77.                             if (p.Count > index && p[index] >= 0) p.RemoveAt(index);
  78.                         });
  79.                     }
  80.                     break;
  81.                 case "negative":
  82.                     if (isRow)
  83.                     {
  84.                         rows[index].RemoveAll(p => p < 0);
  85.                     }
  86.                     else
  87.                     {
  88.                         rows.ForEach(p =>
  89.                      {
  90.                          if (p.Count > index && p[index] < 0) p.RemoveAt(index);
  91.                      });
  92.                     }
  93.                     break;
  94.                 case "even":
  95.                     if (isRow)
  96.                     {
  97.                         rows[index].RemoveAll(p => p % 2 == 0);
  98.                     }
  99.                     else
  100.                     {
  101.                         rows.ForEach(p =>
  102.                      {
  103.                          if (p.Count > index && p[index] % 2 == 0) p.RemoveAt(index);
  104.                      });
  105.                     }
  106.                     break;
  107.                 case "odd":
  108.                     if (isRow)
  109.                     {
  110.                         rows[index].RemoveAll(p => p % 2 != 0);
  111.                     }
  112.                     else
  113.                     {
  114.                         rows.ForEach(p =>
  115.                           {
  116.                               if (p.Count > index && p[index] % 2 != 0) p.RemoveAt(index);
  117.                           });
  118.                     }
  119.                     break;
  120.             }
  121.         }
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement