Advertisement
Guest User

Untitled

a guest
Jan 31st, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace LegoBlocks
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. int rows = int.Parse(Console.ReadLine());
  11. int[][] jaggedArray1 = new int[rows][];
  12. int[][] jaggedArray2 = new int[rows][];
  13.  
  14. jaggedArray1 = ReadNumbers(rows, jaggedArray1);
  15. jaggedArray2 = ReadNumbers(rows, jaggedArray2);
  16.  
  17. bool isLego = true;
  18. for (int row = 0; row < rows - 1; row++)
  19. {
  20. if (jaggedArray1[row].Length + jaggedArray2[row].Length != jaggedArray1[row + 1].Length + jaggedArray2[row + 1].Length)
  21. {
  22. isLego = false;
  23. }
  24. }
  25.  
  26. if (isLego)
  27. {
  28. int columns = 0;
  29. for (int row = 0; row < rows; row++)
  30. {
  31. int[] originalArray = jaggedArray2[row];
  32. jaggedArray2[row] = originalArray.Reverse().ToArray();
  33. columns = jaggedArray1[row].Length + jaggedArray2[row].Length;
  34. }
  35.  
  36. int[,] resultMatrix = new int[rows,columns];
  37. for (int row = 0; row < rows; row++)
  38. {
  39. int colIndex = 0;
  40. for (int col = 0; col < columns; col++)
  41. {
  42. if (col < jaggedArray1[row].Length)
  43. {
  44. resultMatrix[row, col] = jaggedArray1[row][col];
  45. }
  46. else
  47. {
  48. resultMatrix[row, col] = jaggedArray2[row][colIndex];
  49. colIndex++;
  50. }
  51. }
  52. }
  53.  
  54. for (int row = 0; row < resultMatrix.GetLength(0); row++)
  55. {
  56. Console.Write("[");
  57. for (int col = 0; col < resultMatrix.GetLength(1); col++)
  58. {
  59. if (col != resultMatrix.GetLength(1) - 1)
  60. {
  61. Console.Write($"{resultMatrix[row, col]}, ");
  62. }
  63. else
  64. {
  65. Console.Write($"{resultMatrix[row, col]}");
  66. }
  67. }
  68. Console.Write("]");
  69. Console.WriteLine();
  70. }
  71. }
  72. else
  73. {
  74. int sum = SumNumbers(rows, jaggedArray1) + SumNumbers(rows, jaggedArray2);
  75. Console.WriteLine($"The total number of cells is: {sum}");
  76. }
  77. }
  78.  
  79. private static int SumNumbers(int rows, int[][] jaggedArray)
  80. {
  81. int sum = 0;
  82. for (int row = 0; row < rows; row++)
  83. {
  84. for (int index = 0; index < jaggedArray[row].Length; index++)
  85. {
  86. sum += jaggedArray[row][index];
  87. }
  88. }
  89. return sum;
  90. }
  91.  
  92. private static int[][] ReadNumbers(int rows, int[][] jaggedArray)
  93. {
  94. for (int row = 0; row < rows; row++)
  95. {
  96. int[] inputArray = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(int.Parse).ToArray();
  97. int length = inputArray.Length;
  98.  
  99. jaggedArray[row] = new int[length];
  100. for (int counter = 0; counter < length; counter++)
  101. {
  102. jaggedArray[row][counter] = inputArray[counter];
  103. }
  104. }
  105. return jaggedArray;
  106. }
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement