Advertisement
n4wn4w

C# [HOMEWORK] - MNOGOMERNI MASIVI, Dictionary,sets

May 11th, 2015
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 30.19 KB | None | 0 0
  1. Problem 1.  Fill the Matrix//////////////////////////////////////////////////////////////////////////
  2.  
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8.  
  9. namespace ConsoleApplication1
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             int rowsAndColsSize = int.Parse(Console.ReadLine());
  16.             char pattern = (char)Console.Read();
  17.  
  18.             int[,] matrix = new int[rowsAndColsSize, rowsAndColsSize];
  19.             int currentNumber = 1;
  20.             if (Char.ToUpper(pattern).Equals('A'))
  21.             {
  22.                 for (int col = 0; col < rowsAndColsSize; col++)
  23.                 {
  24.                     for (int row = 0; row < rowsAndColsSize; row++)
  25.                     {
  26.                         matrix[row, col] = currentNumber;
  27.                         currentNumber++;
  28.                     }
  29.                 }
  30.             }
  31.             else if (Char.ToUpper(pattern).Equals('B'))
  32.             {
  33.                 for (int col = 0; col < rowsAndColsSize; col++)
  34.                 {
  35.                     if (col % 2 == 0)
  36.                     {
  37.                         for (int row = 0; row < rowsAndColsSize; row++)
  38.                         {
  39.                             matrix[row, col] = currentNumber;
  40.                             currentNumber++;
  41.                         }
  42.                     }
  43.                     else
  44.                     {
  45.                         for (int row = rowsAndColsSize - 1; row >= 0; row--)
  46.                         {
  47.                             matrix[row, col] = currentNumber;
  48.                             currentNumber++;
  49.                         }
  50.                     }
  51.                 }
  52.             }
  53.  
  54.             for (int row = 0; row < rowsAndColsSize; row++)
  55.             {
  56.                 for (int col = 0; col < rowsAndColsSize; col++)
  57.                 {
  58.                     Console.Write("{0,2} ", matrix[row, col]);
  59.                 }
  60.                 Console.WriteLine();
  61.             }
  62.         }
  63.     }
  64. }
  65.  
  66.  
  67.  
  68. Problem 2.  Maximal Sum   /////////////////////////////////////////////////////////////////////////////////////////////////////////
  69.  
  70.  
  71. using System;
  72. class MaximalSum
  73. {
  74.     static void Main()
  75.     {
  76.         string[] rowsAndColsInput = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  77.  
  78.         int rows = int.Parse(rowsAndColsInput[0]) + 1;
  79.         int cols = int.Parse(rowsAndColsInput[1]) + 1;
  80.         int[,] matrix = new int[rows, cols];
  81.         for (int row = 0; row < rows; row++)
  82.         {
  83.             string[] currentRowNumbersAsStrings =
  84.                         Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  85.             for (int col = 0; col < cols; col++)
  86.             {
  87.                 matrix[row, col] = int.Parse(currentRowNumbersAsStrings[col]);
  88.             }
  89.         }
  90.  
  91.         int bestSum = int.MinValue;
  92.         int currentSum = 0;
  93.         int[,] elementsWithMaxSum = new int[3, 3];
  94.         for (int row = 0; row < rows - 2; row++)
  95.         {
  96.             for (int col = 0; col < cols - 2; col++)
  97.             {
  98.                 currentSum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] +
  99.                              matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
  100.                              matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
  101.                 if (currentSum > bestSum)
  102.                 {
  103.                     bestSum = currentSum;
  104.                     for (int i = 0; i < elementsWithMaxSum.GetLength(0); i++)
  105.                     {
  106.                         for (int j = 0; j < elementsWithMaxSum.GetLength(1); j++)
  107.                         {
  108.                             elementsWithMaxSum[i, j] = matrix[row + i, col + j];
  109.                         }
  110.                     }
  111.                 }
  112.             }
  113.         }
  114.  
  115.         Console.WriteLine("Sum = {0}", bestSum);
  116.         for (int row = 0; row < elementsWithMaxSum.GetLength(0); row++)
  117.         {
  118.             for (int col = 0; col < elementsWithMaxSum.GetLength(1); col++)
  119.             {
  120.                 Console.Write("{0,2} ", elementsWithMaxSum[row, col]);
  121.             }
  122.             Console.WriteLine();
  123.         }
  124.     }
  125. }
  126.  
  127.  
  128.  
  129. Problem 3.  Matrix shuffling /////////////////////////////////////////////////////////////////////////////////
  130.  
  131.  
  132. using System;
  133. class MatrixShuffling
  134. {
  135.     static void Main()
  136.     {
  137.         int rows = int.Parse(Console.ReadLine());
  138.         int cols = int.Parse(Console.ReadLine());
  139.  
  140.         string[,] matrix = new string[rows, cols];
  141.         for (int row = 0; row < rows; row++)
  142.         {
  143.             for (int col = 0; col < cols; col++)
  144.             {
  145.                 matrix[row, col] = Console.ReadLine();
  146.             }
  147.         }
  148.  
  149.         int x1 = 0;
  150.         int y1 = 0;
  151.         int x2 = 0;
  152.         int y2 = 0;
  153.         string temp = String.Empty;
  154.         string command = Console.ReadLine();
  155.         while (command != "END")
  156.         {
  157.             string[] commandTokens = command.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  158.             if (commandTokens.Length == 5 && commandTokens[0] == "swap")
  159.             {
  160.                 x1 = int.Parse(commandTokens[1]);
  161.                 y1 = int.Parse(commandTokens[2]);
  162.                 x2 = int.Parse(commandTokens[3]);
  163.                 y2 = int.Parse(commandTokens[4]);
  164.  
  165.                 if ((x1 >= 0 && x1 < rows) && (y1 >= 0 && y1 < cols)
  166.                     && (x2 >= 0 && x2 < rows) && (y2 >= 0 && y2 < cols))
  167.                 {
  168.                     temp = matrix[x1, y1];
  169.                     matrix[x1, y1] = matrix[x2, y2];
  170.                     matrix[x2, y2] = temp;
  171.  
  172.                     Console.WriteLine("(After swapping {0} and {1}): ", matrix[x2, y2], matrix[x1, y1]);
  173.                     for (int row = 0; row < rows; row++)
  174.                     {
  175.                         for (int col = 0; col < cols; col++)
  176.                         {
  177.                             Console.Write("{0,2} ", matrix[row, col]);
  178.                         }
  179.                         Console.WriteLine();
  180.                     }
  181.                 }
  182.                 else
  183.                 {
  184.                     Console.WriteLine("Invalid input!");
  185.                 }
  186.             }
  187.             else
  188.             {
  189.                 Console.WriteLine("Invalid input!");
  190.             }
  191.  
  192.             command = Console.ReadLine();
  193.         }
  194.     }
  195. }
  196.  
  197.  
  198. Problem 4.  Sequence in Matrix  /////////////////////////////////////////////////////////////////////////////////////////
  199.  
  200. using System;
  201. using System.Collections.Generic;
  202.  
  203. class SequenceInMatrix
  204. {
  205.     static void Main()
  206.     {
  207.         List<List<string>> matrix = new List<List<string>>();
  208.  
  209.         string currentRowContents = Console.ReadLine();
  210.         string[] currentRowTokens;
  211.         for (int row = 0; !String.IsNullOrEmpty(currentRowContents); row++)
  212.         {
  213.             matrix.Add(new List<string>());
  214.             currentRowTokens = currentRowContents.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  215.             for (int col = 0; col < currentRowTokens.Length; col++)
  216.             {
  217.                 matrix[row].Add(currentRowTokens[col]);
  218.             }
  219.  
  220.             currentRowContents = Console.ReadLine();
  221.         }
  222.  
  223.         int currentLength = 1;
  224.         int bestLength = int.MinValue;
  225.         string bestValue = String.Empty;
  226.         for (int row = 0; row < matrix.Count; row++)
  227.         {
  228.             for (int col = 0; col < matrix[row].Count; col++)
  229.             {
  230.                 currentLength = CheckVertical(matrix, row, col, matrix[row][col]);
  231.                 if (currentLength > bestLength)
  232.                 {
  233.                     bestLength = currentLength;
  234.                     bestValue = matrix[row][col];
  235.                 }
  236.                 currentLength = CheckHorizontal(matrix, row, col, matrix[row][col]);
  237.                 if (currentLength > bestLength)
  238.                 {
  239.                     bestLength = currentLength;
  240.                     bestValue = matrix[row][col];
  241.                 }
  242.                 currentLength = CheckDiagonal(matrix, row, col, matrix[row][col]);
  243.                 if (currentLength > bestLength)
  244.                 {
  245.                     bestLength = currentLength;
  246.                     bestValue = matrix[row][col];
  247.                 }
  248.             }
  249.         }
  250.  
  251.         Console.Write(bestValue);
  252.         for (int i = 1; i < bestLength; i++)
  253.         {
  254.             Console.Write(", {0}", bestValue);
  255.         }
  256.         Console.WriteLine();
  257.     }
  258.  
  259.     private static int CheckDiagonal(List<List<string>> matrix, int row, int col, string currentValue)
  260.     {
  261.         int currentLength = 1;
  262.         for (int i = row, j = col; i < matrix.Count && j < matrix[row].Count; i++, j++)
  263.         {
  264.             if (i + 1 < matrix.Count && j + 1 < matrix[i + 1].Count && matrix[i + 1][j + 1].Equals(currentValue))
  265.             {
  266.                 currentLength++;
  267.             }
  268.             else
  269.             {
  270.                 break;
  271.             }
  272.         }
  273.         return currentLength;
  274.     }
  275.  
  276.     private static int CheckHorizontal(List<List<string>> matrix, int row, int col, string currentValue)
  277.     {
  278.         int currentLength = 1;
  279.         for (int i = col; i < matrix[row].Count; i++)
  280.         {
  281.             if (i + 1 < matrix[row].Count && matrix[row][i + 1].Equals(currentValue))
  282.             {
  283.                 currentLength++;
  284.             }
  285.             else
  286.             {
  287.                 break;
  288.             }
  289.         }
  290.         return currentLength;
  291.     }
  292.  
  293.     private static int CheckVertical(List<List<string>> matrix, int row, int col, string currentValue)
  294.     {
  295.         int currentLength = 1;
  296.         for (int i = row; i < matrix.Count; i++)
  297.         {
  298.             if (i + 1 < matrix.Count && matrix[i + 1][col].Equals(currentValue))
  299.             {
  300.                 currentLength++;
  301.             }
  302.             else
  303.             {
  304.                 break;
  305.             }
  306.         }
  307.         return currentLength;
  308.     }
  309. }
  310.  
  311. Problem 5.  Collect the Coins   ///////////////////////////////////////////////////////////////////////////////
  312.  
  313. using System;
  314. class CollectTheCoins
  315. {
  316.     static void Main()
  317.     {
  318.         char[][] board = new char[4][];
  319.  
  320.         string boardLine = String.Empty;
  321.         for (int row = 0; row < board.GetLength(0); row++)
  322.         {
  323.             boardLine = Console.ReadLine();
  324.             board[row] = new char[boardLine.Length];
  325.             for (int col = 0; col < board[row].Length; col++)
  326.             {
  327.                 board[row][col] = boardLine[col];
  328.             }
  329.         }
  330.  
  331.         int currentRow = 0;
  332.         int currentCol = 0;
  333.         int coinsCount = 0;
  334.         int wallHitsCount = 0;
  335.         string movementCommands = Console.ReadLine();
  336.         foreach (char currentDirection in movementCommands)
  337.         {
  338.             if (currentDirection == 'V')
  339.             {
  340.                 currentRow++;
  341.                 if (currentRow >= board.GetLength(0))
  342.                 {
  343.                     currentRow--;
  344.                     wallHitsCount++;
  345.                     continue;
  346.                 }
  347.                 else if (currentCol >= board[currentRow].Length)
  348.                 {
  349.                     currentRow--;
  350.                     wallHitsCount++;
  351.                     continue;
  352.                 }
  353.             }
  354.             else if (currentDirection == '>')
  355.             {
  356.                 currentCol++;
  357.                 if (currentCol >= board[currentRow].Length)
  358.                 {
  359.                     currentCol--;
  360.                     wallHitsCount++;
  361.                     continue;
  362.                 }
  363.             }
  364.             else if (currentDirection == '<')
  365.             {
  366.                 currentCol--;
  367.                 if (currentCol < 0)
  368.                 {
  369.                     currentCol++;
  370.                     wallHitsCount++;
  371.                     continue;
  372.                 }
  373.             }
  374.             else if (currentDirection == '^')
  375.             {
  376.                 currentRow--;
  377.                 if (currentRow < 0)
  378.                 {
  379.                     currentRow++;
  380.                     wallHitsCount++;
  381.                     continue;
  382.                 }
  383.                 else if (currentCol >= board[currentRow].Length)
  384.                 {
  385.                     currentRow++;
  386.                     wallHitsCount++;
  387.                     continue;
  388.                 }
  389.             }
  390.  
  391.             if (board[currentRow][currentCol].Equals('$'))
  392.             {
  393.                 coinsCount++;
  394.             }
  395.         }
  396.         Console.WriteLine("Coins collected: {0}", coinsCount);
  397.         Console.WriteLine("Walls hit: {0}", wallHitsCount);
  398.     }
  399. }
  400.  
  401. Problem 6.  Count Symbols  ////////////////////////////////////////////////////////////////////////////////////////
  402.  
  403. using System;
  404. using System.Collections.Generic;
  405.  
  406. class CountSymbols
  407. {
  408.     static void Main()
  409.     {
  410.         string inputText = Console.ReadLine();
  411.  
  412.         SortedDictionary<char, int> occurrences = new SortedDictionary<char, int>();
  413.         foreach (char symbol in inputText)
  414.         {
  415.             if (occurrences.ContainsKey(symbol))
  416.             {
  417.                 occurrences[symbol]++;
  418.             }
  419.             else
  420.             {
  421.                 occurrences.Add(symbol, 1);
  422.             }
  423.         }
  424.  
  425.         foreach (KeyValuePair<char, int> pair in occurrences)
  426.         {
  427.             Console.WriteLine("{0}: {1} time/s", pair.Key, pair.Value);
  428.         }
  429.     }
  430. }
  431.  
  432.  
  433. Problem 7.  Phonebook  ////////////////////////////////////////////////////////////////////////
  434.  
  435. using System;
  436. using System.Collections.Generic;
  437. class Phonebook
  438. {
  439.     static void Main()
  440.     {
  441.         Dictionary<string, List<string>> phonebook = new Dictionary<string, List<string>>();
  442.  
  443.         string contactInformation = Console.ReadLine();
  444.         string[] contactTokens;
  445.         string contactName = String.Empty;
  446.         string contactNumber = String.Empty;
  447.  
  448.         while (contactInformation != "search")
  449.         {
  450.             contactTokens = contactInformation.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
  451.             contactName = contactTokens[0];
  452.             contactNumber = contactTokens[1];
  453.             if (!phonebook.ContainsKey(contactName))
  454.             {
  455.                 phonebook[contactName] = new List<string>();
  456.  
  457.             }
  458.             phonebook[contactName].Add(contactNumber);
  459.  
  460.             contactInformation = Console.ReadLine();
  461.         }
  462.  
  463.         string contactNameToSearch = Console.ReadLine();
  464.         while (!String.IsNullOrEmpty(contactNameToSearch))
  465.         {
  466.             if (phonebook.ContainsKey(contactNameToSearch))
  467.             {
  468.                 Console.Write("{0} -> {1}", contactNameToSearch, String.Join(", ", phonebook[contactNameToSearch]));
  469.             }
  470.             else
  471.             {
  472.                 Console.WriteLine("Contact {0} does not exist.", contactNameToSearch);
  473.             }
  474.  
  475.             contactNameToSearch = Console.ReadLine();
  476.         }
  477.     }
  478. }
  479.  
  480.  
  481. Problem 8.  Night Life   //////////////////////////////////////////////////////////////////////////////////////////////////////
  482.  
  483.  
  484. using System;
  485. using System.Collections.Generic;
  486. class NightLife
  487. {
  488.     static void Main()
  489.     {
  490.         Dictionary<string, SortedDictionary<string, SortedSet<string>>> nightLifeDictionary =
  491.                             new Dictionary<string, SortedDictionary<string, SortedSet<string>>>();
  492.  
  493.         string[] eventTokens;
  494.         string city = String.Empty;
  495.         string venue = String.Empty;
  496.         string performer = String.Empty;
  497.         string eventInformation = Console.ReadLine();
  498.  
  499.         while (eventInformation != "END")
  500.         {
  501.             eventTokens = eventInformation.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  502.  
  503.             city = eventTokens[0];
  504.             venue = eventTokens[1];
  505.             performer = eventTokens[2];
  506.  
  507.             if (!nightLifeDictionary.ContainsKey(city))
  508.             {
  509.                 nightLifeDictionary[city] = new SortedDictionary<string, SortedSet<string>>();
  510.             }
  511.             if (!nightLifeDictionary[city].ContainsKey(venue))
  512.             {
  513.                 nightLifeDictionary[city][venue] = new SortedSet<string>();
  514.             }
  515.             nightLifeDictionary[city][venue].Add(performer);
  516.  
  517.             eventInformation = Console.ReadLine();
  518.         }
  519.  
  520.         foreach (var cityPair in nightLifeDictionary)
  521.         {
  522.             Console.WriteLine(cityPair.Key);
  523.             foreach (var venuePair in cityPair.Value)
  524.             {
  525.                 Console.WriteLine("->{0}: {1}", venuePair.Key, String.Join(", ", venuePair.Value));
  526.             }
  527.         }
  528.     }
  529. }
  530.  
  531.  
  532. 9 problem  Terrorists Win!  /////////////////////////////////////////////////////////////////////////////////////////
  533.  
  534.  
  535. using System;
  536. using System.Text;
  537.  
  538. class TerroristsWin
  539. {
  540.     static void Main()
  541.     {
  542.         string inputText = Console.ReadLine();
  543.  
  544.         StringBuilder inputTextAsStringBuilder = new StringBuilder(inputText);
  545.         int startIndex = inputText.IndexOf('|');
  546.         int endIndex = 0;
  547.         int unicodeSum = 0;
  548.         int destroyedArea = 0;
  549.         while (startIndex != -1)
  550.         {
  551.             unicodeSum = 0;
  552.             endIndex = inputText.IndexOf('|', startIndex + 1);
  553.             if (endIndex <= startIndex)
  554.             {
  555.                 break;
  556.             }
  557.             for (int i = startIndex + 1; i <= endIndex - 1; i++)
  558.             {
  559.                 unicodeSum += inputText[i];
  560.                 inputTextAsStringBuilder[i] = '.';
  561.             }
  562.             destroyedArea = unicodeSum % 10;
  563.  
  564.             for (int i = startIndex; i >= startIndex - destroyedArea && i >= 0; i--)
  565.             {
  566.                 inputTextAsStringBuilder[i] = '.';
  567.             }
  568.             for (int i = endIndex; i <= endIndex + destroyedArea && i < inputTextAsStringBuilder.Length; i++)
  569.             {
  570.                 inputTextAsStringBuilder[i] = '.';
  571.             }
  572.  
  573.             startIndex = inputText.IndexOf('|', endIndex + 1);
  574.         }
  575.         Console.WriteLine(inputTextAsStringBuilder);
  576.     }
  577. }
  578.  
  579.  
  580. Problem 10.  * Plus-Remove   ///////////////////////////////////////////////////////////////////////////////
  581.  
  582.  
  583. using System;
  584. using System.Collections.Generic;
  585.  
  586. class PlusRemove
  587. {
  588.     static void Main()
  589.     {
  590.         List<List<char>> board = new List<List<char>>();
  591.  
  592.         string lineContents = Console.ReadLine();
  593.         for (int row = 0; lineContents != "END"; row++)
  594.         {
  595.             board.Add(new List<char>(lineContents.Length));
  596.             for (int col = 0; col < lineContents.Length; col++)
  597.             {
  598.                 board[row].Add(lineContents[col]);
  599.             }
  600.             lineContents = Console.ReadLine();
  601.         }
  602.  
  603.         char currentValue = '\0';
  604.         HashSet<KeyValuePair<int, int>> coordinatesSet = new HashSet<KeyValuePair<int, int>>();
  605.         for (int row = 1; row < board.Count - 1; row++)
  606.         {
  607.             for (int col = 1; col < board[row].Count - 1; col++)
  608.             {
  609.                 currentValue = Char.ToLower(board[row][col]);
  610.                 if (col < board[row - 1].Count && currentValue.Equals(Char.ToLower(board[row - 1][col]))
  611.                     && currentValue.Equals(Char.ToLower(board[row][col - 1])) && currentValue.Equals(Char.ToLower(board[row][col + 1]))
  612.                     && col < board[row + 1].Count && currentValue.Equals(Char.ToLower(board[row + 1][col])))
  613.                 {
  614.                     coordinatesSet.Add(new KeyValuePair<int, int>(row, col));
  615.                     coordinatesSet.Add(new KeyValuePair<int, int>(row - 1, col));
  616.                     coordinatesSet.Add(new KeyValuePair<int, int>(row + 1, col));
  617.                     coordinatesSet.Add(new KeyValuePair<int, int>(row, col + 1));
  618.                     coordinatesSet.Add(new KeyValuePair<int, int>(row, col - 1));
  619.                 }
  620.             }
  621.         }
  622.  
  623.         for (int row = 0; row < board.Count; row++)
  624.         {
  625.             for (int col = 0; col < board[row].Count; col++)
  626.             {
  627.                 KeyValuePair<int, int> curKeyValuePair = new KeyValuePair<int, int>(row, col);
  628.                 if (!coordinatesSet.Contains(curKeyValuePair))
  629.                 {
  630.                     Console.Write(board[row][col]);
  631.                 }
  632.             }
  633.             Console.WriteLine();
  634.         }
  635.     }
  636. }
  637.  
  638.  
  639. Problem 11.  * String Matrix Rotation   ////////////////////////////////////////////////////////////////////////////////////
  640.  
  641.  
  642. using System;
  643. using System.Collections.Generic;
  644.  
  645. class StringMatrixRotation
  646. {
  647.     static void Main()
  648.     {
  649.         string[] inputTokens = Console.ReadLine().Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
  650.         int degrees = int.Parse(inputTokens[1]) % 360;
  651.  
  652.         int bestLength = int.MinValue;
  653.         Queue<string> elements = new Queue<string>();
  654.         string currentElement = Console.ReadLine();
  655.         while (!String.IsNullOrEmpty(currentElement))
  656.         {
  657.             elements.Enqueue(currentElement);
  658.             if (currentElement.Length > bestLength)
  659.             {
  660.                 bestLength = currentElement.Length;
  661.             }
  662.             currentElement = Console.ReadLine();
  663.         }
  664.  
  665.         char[,] matrix = new char[elements.Count, bestLength];
  666.         for (int row = 0; row < matrix.GetLength(0); row++)
  667.         {
  668.             currentElement = elements.Dequeue().PadRight(bestLength, ' ');
  669.             for (int col = 0; col < matrix.GetLength(1); col++)
  670.             {
  671.                 matrix[row, col] = currentElement[col];
  672.             }
  673.         }
  674.  
  675.         if (degrees == 90)
  676.         {
  677.             char[,] rotatedMatrix = new char[matrix.GetLength(1), matrix.GetLength(0)];
  678.  
  679.             for (int row = 0; row < rotatedMatrix.GetLength(0); row++)
  680.             {
  681.                 for (int col = 0; col < rotatedMatrix.GetLength(1); col++)
  682.                 {
  683.                     rotatedMatrix[row, col] = matrix[matrix.GetLength(0) - col - 1, row];
  684.                 }
  685.             }
  686.             PrintMatrix(rotatedMatrix);
  687.         }
  688.         else if (degrees == 180)
  689.         {
  690.             char[,] rotatedMatrix = new char[matrix.GetLength(0), matrix.GetLength(1)];
  691.  
  692.             for (int row = 0; row < rotatedMatrix.GetLength(0); row++)
  693.             {
  694.                 for (int col = 0; col < rotatedMatrix.GetLength(1); col++)
  695.                 {
  696.                     rotatedMatrix[row, col] = matrix[matrix.GetLength(0) - row - 1, matrix.GetLength(1) - col - 1];
  697.                 }
  698.             }
  699.             PrintMatrix(rotatedMatrix);
  700.         }
  701.         else if (degrees == 270)
  702.         {
  703.             char[,] rotatedMatrix = new char[matrix.GetLength(1), matrix.GetLength(0)];
  704.  
  705.             for (int col = 0; col < rotatedMatrix.GetLength(1); col++)
  706.             {
  707.                 for (int row = 0; row < rotatedMatrix.GetLength(0); row++)
  708.                 {
  709.                     rotatedMatrix[row, col] = matrix[col, matrix.GetLength(1) - row - 1];
  710.                 }
  711.             }
  712.             PrintMatrix(rotatedMatrix);
  713.         }
  714.         else if (degrees == 0)
  715.         {
  716.             PrintMatrix(matrix);
  717.         }
  718.     }
  719.  
  720.     private static void PrintMatrix(char[,] matrix)
  721.     {
  722.         for (int row = 0; row < matrix.GetLength(0); row++)
  723.         {
  724.             for (int col = 0; col < matrix.GetLength(1); col++)
  725.             {
  726.                 Console.Write(matrix[row, col]);
  727.             }
  728.             Console.WriteLine();
  729.         }
  730.     }
  731. }
  732.  
  733.  
  734. Problem 12.  * To the Stars!     /////////////////////////////////////////////////////////////////////////////////////////////
  735.  
  736.  
  737.  
  738. using System;
  739. using System.Collections.Generic;
  740.  
  741. class ToTheStars
  742. {
  743.     static void Main()
  744.     {
  745.         Dictionary<string, Tuple<double, double>> starSystemsDictionary = new Dictionary<string, Tuple<double, double>>();
  746.  
  747.         string startSystemName = String.Empty;
  748.         double starSystemCoordinateX = 0;
  749.         double starSystemCoordinateY = 0;
  750.         string[] starSystemsTokens;
  751.         for (int i = 0; i < 3; i++)
  752.         {
  753.             starSystemsTokens = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  754.             startSystemName = starSystemsTokens[0];
  755.             starSystemCoordinateX = double.Parse(starSystemsTokens[1]);
  756.             starSystemCoordinateY = double.Parse(starSystemsTokens[2]);
  757.  
  758.             starSystemsDictionary[startSystemName] = new Tuple<double, double>(starSystemCoordinateX, starSystemCoordinateY);
  759.         }
  760.  
  761.         string[] spaceshipStartCordinates = Console.ReadLine()
  762.             .Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  763.         int numberOfMoves = int.Parse(Console.ReadLine());
  764.         double spaceshipX = double.Parse(spaceshipStartCordinates[0]);
  765.         double spaceshipY = double.Parse(spaceshipStartCordinates[1]);
  766.  
  767.         bool isInStarSystem = false;
  768.         while (numberOfMoves >= 0)
  769.         {
  770.             isInStarSystem = false;
  771.             foreach (var pair in starSystemsDictionary)
  772.             {
  773.                 if (IsInStarSystem(pair.Value.Item1, pair.Value.Item2, spaceshipX, spaceshipY))
  774.                 {
  775.                     Console.WriteLine(pair.Key.ToLower());
  776.                     isInStarSystem = true;
  777.                     break;
  778.                 }
  779.             }
  780.  
  781.             if (!isInStarSystem)
  782.             {
  783.                 Console.WriteLine("space");
  784.             }
  785.  
  786.             spaceshipY++;
  787.             numberOfMoves--;
  788.         }
  789.     }
  790.  
  791.     private static bool IsInStarSystem(double starX, double starY, double spaceshipX, double spaceshipY)
  792.     {
  793.         if ((spaceshipX >= starX - 1 && spaceshipX <= starX + 1) && (spaceshipY >= starY - 1 && spaceshipY <= starY + 1))
  794.         {
  795.             return true;
  796.         }
  797.         else
  798.         {
  799.             return false;
  800.         }
  801.     }
  802. }
  803.  
  804.  
  805. Problem 13.  * Activity Tracker   ////////////////////////////////////////////////////////////////////////////////////////////
  806.  
  807.  
  808. using System;
  809. using System.Collections.Generic;
  810.  
  811. class ActivityTracker
  812. {
  813.     static void Main()
  814.     {
  815.         SortedDictionary<int, SortedDictionary<string, int>> dataDictionary =
  816.                                             new SortedDictionary<int, SortedDictionary<string, int>>();
  817.  
  818.         int dataLinesNumber = int.Parse(Console.ReadLine());
  819.         string lineContents = String.Empty;
  820.         string[] lineTokens;
  821.         int month = 0;
  822.         string user = String.Empty;
  823.         int distance = 0;
  824.         for (int i = 0; i < dataLinesNumber; i++)
  825.         {
  826.             lineContents = Console.ReadLine();
  827.             lineTokens = lineContents.Split(new char[] { ' ', '/' }, StringSplitOptions.RemoveEmptyEntries);
  828.             month = int.Parse(lineTokens[1]);
  829.             user = lineTokens[3];
  830.             distance = int.Parse(lineTokens[4]);
  831.  
  832.             if (!dataDictionary.ContainsKey(month))
  833.             {
  834.                 dataDictionary[month] = new SortedDictionary<string, int>();
  835.             }
  836.  
  837.             if (!dataDictionary[month].ContainsKey(user))
  838.             {
  839.                 dataDictionary[month][user] = distance;
  840.             }
  841.             else
  842.             {
  843.                 dataDictionary[month][user] += distance;
  844.             }
  845.         }
  846.  
  847.         bool isFirstPair = true;
  848.         foreach (var dataPair in dataDictionary)
  849.         {
  850.             Console.Write("{0}: ", dataPair.Key);
  851.             isFirstPair = true;
  852.             foreach (var subPair in dataPair.Value)
  853.             {
  854.                 if (isFirstPair)
  855.                 {
  856.                     Console.Write("{0}({1})", subPair.Key, subPair.Value);
  857.                     isFirstPair = false;
  858.                 }
  859.                 else
  860.                 {
  861.                     Console.Write(", {0}({1})", subPair.Key, subPair.Value);
  862.                 }
  863.             }
  864.             Console.WriteLine();
  865.         }
  866.     }
  867. }
  868.  
  869.  
  870.  
  871.  
  872.  
  873.  
  874.  
  875. using System;
  876. using System.Collections.Generic;
  877. class NightLife
  878. {
  879.     static void Main()
  880.     {
  881.         Dictionary<string, SortedDictionary<string, List<int>>> nightLifeDictionary =
  882.                             new Dictionary<string, SortedDictionary<string, List<int>>>();
  883.  
  884.         string[] eventTokens;
  885.         string city = String.Empty;
  886.         string venue = String.Empty;
  887.         int performer = 0;
  888.         string eventInformation = Console.ReadLine();
  889.  
  890.         while (eventInformation != "END")
  891.         {
  892.             eventTokens = eventInformation.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  893.  
  894.             city = eventTokens[0];
  895.             venue = eventTokens[1];
  896.             performer = int.Parse(eventTokens[2]);
  897.  
  898.             if (!nightLifeDictionary.ContainsKey(city))
  899.             {
  900.                 nightLifeDictionary[city] = new SortedDictionary<string, List<int>>();
  901.             }
  902.             if (!nightLifeDictionary[city].ContainsKey(venue))
  903.             {
  904.                 nightLifeDictionary[city][venue] = new List<int>();
  905.             }
  906.             nightLifeDictionary[city][venue].Add(performer);
  907.  
  908.             eventInformation = Console.ReadLine();
  909.         }
  910.  
  911.         string kondio = "";
  912.         double sum = 0;
  913.         double count = 0;
  914.         double avg = 0;
  915.         foreach (var cityPair in nightLifeDictionary)
  916.         {
  917.             Console.WriteLine();
  918.             Console.Write(cityPair.Key + ":: ");
  919.            // Console.WriteLine(": " + kondio);
  920.            // Console.WriteLine(avg);
  921.             foreach (var venuePair in cityPair.Value)
  922.             {
  923.                
  924.                 Console.Write(" ->{0}: ", venuePair.Key);
  925.                 count = 0;
  926.                 sum = 0;
  927.                 foreach (var item in venuePair.Value)
  928.                 {
  929.                     count++;
  930.                   //  Console.WriteLine(item);
  931.                     sum += item;
  932.  
  933.                 }
  934.                 venuePair.Value.ToString();
  935.                 avg = sum / count;
  936.               //  Console.WriteLine("->{0}: ", venuePair.Key);
  937.                 kondio = venuePair.Key.ToString();
  938.                 Console.Write(" >>" + avg);
  939.                
  940.             }
  941.             Console.WriteLine();
  942.         }
  943.     }
  944. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement