Advertisement
Guest User

Untitled

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