Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace MatrixOP
- {
- class Program
- {
- static List<List<int>> rows = new List<List<int>>();
- static void Main(string[] args)
- {
- int rowsCount = int.Parse(Console.ReadLine());
- for (int i = 0; i < rowsCount; i++)
- {
- rows.Add(Console.ReadLine().Split().Select(int.Parse).ToList());
- }
- string input = Console.ReadLine();
- while (input != "end")
- {
- string[] inputArgs = input.Split();
- switch (inputArgs[0])
- {
- case "remove":
- RemoveCommand(inputArgs);
- break;
- case "insert":
- InsertCommand(inputArgs);
- break;
- case "swap":
- SwapCommand(inputArgs);
- break;
- }
- input = Console.ReadLine();
- }
- foreach (List<int> row in rows)
- {
- Console.WriteLine(string.Join(" ", row));
- }
- }
- private static void SwapCommand(string[] input)
- {
- int first = int.Parse(input[1]);
- int second = int.Parse(input[2]);
- List<int> oldFirst = new List<int>(rows[first]);
- List<int> oldSecond = new List<int>(rows[second]);
- rows[first] = oldSecond;
- rows[second] = oldFirst;
- }
- private static void InsertCommand(string[] input)
- {
- int row = int.Parse(input[1]);
- int element = int.Parse(input[2]);
- rows[row].Insert(0, element);
- }
- private static void RemoveCommand(string[] input)
- {
- bool isRow = input[2] == "row";
- int index = int.Parse(input[3]);
- switch (input[1])
- {
- case "positive":
- if (isRow)
- {
- rows[index].RemoveAll(p => p >= 0);
- }
- else
- {
- rows.ForEach(p =
- {
- if (p.Count > index && p[index] >= 0) p.RemoveAt(index);
- });
- }
- break;
- case "negative":
- if (isRow)
- {
- rows[index].RemoveAll(p => p < 0);
- }
- else
- {
- rows.ForEach(p =>
- {
- if (p.Count > index && p[index] < 0) p.RemoveAt(index);
- });
- }
- break;
- case "even":
- if (isRow)
- {
- rows[index].RemoveAll(p => p % 2 == 0);
- }
- else
- {
- rows.ForEach(p =>
- {
- if (p.Count > index && p[index] % 2 == 0) p.RemoveAt(index);
- });
- }
- break;
- case "odd":
- if (isRow)
- {
- rows[index].RemoveAll(p => p % 2 != 0);
- }
- else
- {
- rows.ForEach(p =>
- {
- if (p.Count > index && p[index] % 2 != 0) p.RemoveAt(index);
- });
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement