Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Data.Odbc;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- class Program
- {
- static void Main()
- {
- int rowsCount = int.Parse(Console.ReadLine());
- List<List<long>> matrix = ReadMatrix(rowsCount);
- string input = Console.ReadLine();
- while (input != "end")
- {
- string[] tokens = input.Split();
- if (tokens[0] == "remove")
- {
- Remove(tokens, matrix);
- }
- else if (tokens[0] == "swap")
- {
- Swap(tokens, matrix);
- }
- else if (tokens[0] == "insert")
- {
- Insert(tokens, matrix);
- }
- input = Console.ReadLine();
- }
- foreach (var row in matrix)
- {
- Console.WriteLine($"{string.Join(" ", row)}");
- }
- }
- private static void Remove(string[] tokens, List<List<long>> matrix)
- {
- string type = tokens[1];
- string postion = tokens[2];
- int index = int.Parse(tokens[3]);
- if (postion == "row" && index >= 0 && index < matrix.Count)
- {
- switch (type)
- {
- case "odd":
- matrix[index].RemoveAll(n => n % 2 != 0);
- break;
- case "even":
- matrix[index].RemoveAll(n => n % 2 == 0);
- break;
- case "negative":
- matrix[index].RemoveAll(n => n < 0);
- break;
- case "positive":
- matrix[index].RemoveAll(n => n >= 0);
- break;
- }
- }
- else if (postion == "col")
- {
- foreach (List<long> row in matrix)
- {
- if (index >= 0 && index < row.Count)
- {
- if (type == "odd" && row[index] % 2 != 0)
- {
- row.RemoveAt(index);
- }
- else if (type == "even" && row[index] % 2 == 0)
- {
- row.RemoveAt(index);
- }
- else if (type == "negative" && row[index] < 0)
- {
- row.RemoveAt(index);
- }
- else if (type == "positive" && row[index] >= 0)
- {
- row.RemoveAt(index);
- }
- }
- }
- }
- }
- private static void Swap(string[] tokens, List<List<long>> matrix)
- {
- int firstRow = int.Parse(tokens[1]);
- int secondRow = int.Parse(tokens[2]);
- if (firstRow >= 0 && firstRow < matrix.Count && secondRow >= 0 && secondRow < matrix.Count)
- {
- List<long> firstRowTemp = matrix[firstRow].ToList();
- matrix[firstRow] = matrix[secondRow].ToList();
- matrix[secondRow] = firstRowTemp.ToList();
- }
- }
- private static void Insert(string[] tokens, List<List<long>> matrix)
- {
- int row = int.Parse(tokens[1]);
- long element = long.Parse(tokens[2]);
- if (row >= 0 && row < matrix.Count)
- {
- matrix[row].Insert(0, element);
- }
- }
- private static List<List<long>> ReadMatrix(int rowsCount)
- {
- List<List<long>> table = new List<List<long>>();
- for (int i = 0; i < rowsCount; i++)
- {
- List<long> currentRow = Console.ReadLine().Split().Select(long.Parse).ToList();
- table.Add(currentRow);
- }
- return table;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement