Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////// 3 meren masiv cube/kub //////////////////////////////////////////////////////////////
- int [,,] cube = new int[5, 5, 5];
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- for (int k = 0; k < 5; k++)
- {
- cube[i, j, k] = i + j + k;
- }
- }
- }
- for (int i = 0; i < 5; i++)
- {
- for (int j = 0; j < 5; j++)
- {
- for (int k = 0; k < 5; k++)
- {
- Console.Write("{0,4}", cube[i,j,k]);
- }
- Console.WriteLine();
- }
- Console.WriteLine("----------------------------");
- }
- ////////////// matrica 3 X 3 ////////////
- // Declare and initialize the matrix
- int[,] matrix =
- {
- { 0, 2, 4, 0, 9, 5 },
- { 7, 1, 3, 3, 2, 1 },
- { 1, 3, 9, 8, 5, 6 },
- { 4, 6, 7, 9, 1, 0 }
- };
- // Find the maximal sum platform of size 2 x 2
- int bestSum = int.MinValue;
- int bestRow = 0;
- int bestCol = 0;
- for (int row = 0; row < matrix.GetLength(0) - 2; row++)
- {
- for (int col = 0; col < matrix.GetLength(1) - 2; col++)
- {
- int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 2, col + 2] + matrix[row, col + 2] + matrix[row + 2, col] + +matrix[row + 2, col + 1] + matrix[row + 1, col + 2];
- if (sum > bestSum)
- {
- bestSum = sum;
- bestRow = row;
- bestCol = col;
- }
- }
- }
- // Print the result
- Console.WriteLine("The best platform is:");
- Console.WriteLine(" {0} {1} {2}", matrix[bestRow, bestCol], matrix[bestRow, bestCol + 1], matrix[bestRow, bestCol + 2]);
- Console.WriteLine(" {0} {1} {2}", matrix[bestRow + 1, bestCol], matrix[bestRow + 1, bestCol + 1], matrix[bestRow + 1, bestCol + 2]);
- Console.WriteLine(" {0} {1} {2}", matrix[bestRow + 2, bestCol], matrix[bestRow + 2, bestCol + 1], matrix[bestRow + 2, bestCol + 2]);
- Console.WriteLine("The maximal sum is: {0}", bestSum);
- ///////////////////////// abstrakta matrica ////////////////
- // Declare and initialize the matrix
- int width = 3;
- int height = 2;
- int[,] matrix =
- {
- { 0, 2, 4, 0, 9, 5 },
- { 7, 1, 3, 3, 2, 1 },
- { 1, 3, 9, 8, 5, 6 },
- { 4, 6, 7, 9, 1, 0 }
- };
- // Find the maximal sum platform of size 2 x 2
- int bestSum = int.MinValue;
- int bestRow = 0;
- int bestCol = 0;
- for (int row = 0; row < matrix.GetLength(0) - height +1; row++)
- {
- for (int col = 0; col < matrix.GetLength(1) - width + 1; col++)
- {
- int sum = 0;
- for (int platformRow = row; platformRow < row + height; platformRow++)
- {
- for (int platformCol = col; platformCol < col + width; platformCol++)
- {
- sum += matrix[platformRow, platformCol];
- }
- }
- if (sum > bestSum)
- {
- bestSum = sum;
- bestRow = row;
- bestCol = col;
- }
- }
- }
- // Print the result
- Console.WriteLine("The best platform is:");
- for (int platformRow = bestRow; platformRow < bestRow + height ; platformRow++)
- {
- for (int platformCol = bestCol; platformCol < bestCol + width; platformCol++)
- {
- Console.Write("{0} ", matrix[platformRow , platformCol]);
- }
- Console.WriteLine();
- }
- // Console.WriteLine(" {0} {1}", matrix[bestRow, bestCol], matrix[bestRow, bestCol + 1]);
- // Console.WriteLine(" {0} {1}", matrix[bestRow + 1, bestCol], matrix[bestRow + 1, bestCol + 1]);
- Console.WriteLine("The maximal sum is: {0}", bestSum);
- /////////////////////////////// 2 reshenie na gornata zadacha /////////////////////////////////////////////////////
- int height = 3;
- int width = 3;
- // Declare and initialize the matrix
- int[,] matrix =
- {
- { 0, 2, 4, 0, 9, 5 },
- { 7, 1, 3, 3, 2, 1 },
- { 1, 3, 9, 8, 5, 6 },
- { 4, 6, 7, 9, 1, 0 }
- };
- // Find the maximal sum platform of size 2 x 2
- int bestSum = int.MinValue;
- int bestRow = 0;
- int bestCol = 0;
- for (int row = 0; row < matrix.GetLength(0) - 2; row++)
- {
- for (int col = 0; col < matrix.GetLength(1) - 2; col++)
- {
- int sum = matrix[row + 0, col +0]
- + matrix[row + 0, col + 1]
- + matrix[row + 1, col + 0]
- + matrix[row + 1, col + 1]
- + matrix[row + 2, col + 0]
- + matrix[row + 0, col + 2]
- + matrix[row + 1, col + 2]
- + matrix[row + 2, col + 1]
- + matrix[row + 2, col + 2]
- ;
- if (sum > bestSum)
- {
- bestSum = sum;
- bestRow = row;
- bestCol = col;
- }
- }
- }
- Console.WriteLine("The best platform is: "+ bestSum);
- for (int platformRow = bestRow; platformRow < bestRow + height; platformRow++)
- {
- for (int platformCol = bestCol; platformCol < bestCol + width; platformCol++)
- {
- Console.Write("{0} ", matrix[platformRow, platformCol]);
- }
- Console.WriteLine();
- }
- ///////////////////// nazubeni masivi /////////////
- int[] numbers = { 0, 1, 4, 113, 55, 3, 1, 2, 66, 557, 124, 2 };
- int[] sizes = new int[3];
- int[] offsets = new int[3];
- // Calculate the sizes for each reminder (0, 1 and 2)
- foreach (var number in numbers)
- {
- int remainder = number % 3;
- sizes[remainder]++;
- }
- // Calculate the list of numbers for each reminder (0, 1 and 2)
- int[][] numbersByRemainder = new int[3][] { new int[sizes[0]], new int[sizes[1]], new int[sizes[2]] };
- foreach (var number in numbers)
- {
- int remainder = number % 3;
- int index = offsets[remainder];
- numbersByRemainder[remainder][index] = number;
- offsets[remainder]++;
- }
- // Print the result jagged array
- for (int row = 0; row < numbersByRemainder.GetLength(0); row++)
- {
- foreach (var num in numbersByRemainder[row])
- {
- Console.Write(num + " ");
- }
- Console.WriteLine();
- }
- /////////////// nazubeni masivi abstraktno ///////////////////////////
- int n = 4;
- int[] numbers = { 0, 1, 4, 113, 55, 3, 1, 2, 66, 557, 124, 2 };
- int[] sizes = new int[n];
- int[] offsets = new int[n];
- // Calculate the sizes for each reminder (0, 1 and 2)
- foreach (var number in numbers)
- {
- int remainder = number % n;
- sizes[remainder]++;
- }
- // Calculate the list of numbers for each reminder (0, 1 and 2)
- int[][] numbersByRemainder = new int[n][];
- for (int i = 0; i < n; i++)
- {
- numbersByRemainder[i] = new int[sizes[i]];
- }
- foreach (var number in numbers)
- {
- int remainder = number % n;
- int index = offsets[remainder];
- numbersByRemainder[remainder][index] = number;
- offsets[remainder]++;
- }
- // Print the result jagged array
- for (int row = 0; row < numbersByRemainder.GetLength(0); row++)
- {
- foreach (var num in numbersByRemainder[row])
- {
- Console.Write(num + " ");
- }
- Console.WriteLine();
- }
- ///////////////// umnojenie na matrici ///////////////////////
- int[,] firstMatrix = new int[4, 2] {
- { 1, 2 },
- { 3, 4 },
- { 5, 6 },
- { 7, 8 } };
- // Print the first matrix
- for (int row = 0; row < firstMatrix.GetLength(0); row++)
- {
- for (int col = 0; col < firstMatrix.GetLength(1); col++)
- {
- Console.Write("{0} ", firstMatrix[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- int[,] secondMatrix = new int[2, 3] {
- { 1, 2, 3 },
- { 4, 5, 6 } };
- // Print the second matrix
- for (int row = 0; row < secondMatrix.GetLength(0); row++)
- {
- for (int col = 0; col < secondMatrix.GetLength(1); col++)
- {
- Console.Write("{0} ", secondMatrix[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- // Multiply the first matrix with the second matrix
- int cols1 = firstMatrix.GetLength(1);
- int rows1 = firstMatrix.GetLength(0);
- int cols2 = secondMatrix.GetLength(1);
- int rows2 = secondMatrix.GetLength(0);
- if (cols1 != rows2)
- {
- throw new ArgumentException("Invalid dimensions!");
- }
- int[,] resultMatrix = new int[rows1, cols2];
- for (int row = 0; row < rows1; row++)
- {
- for (int col = 0; col < cols2; col++)
- {
- resultMatrix[row, col] = 0;
- for (int i = 0; i < cols1; i++)
- {
- resultMatrix[row, col] += firstMatrix[row, i] * secondMatrix[i, col];
- }
- }
- }
- // Print the result matrix
- for (int row = 0; row < resultMatrix.GetLength(0); row++)
- {
- for (int col = 0; col < resultMatrix.GetLength(1); col++)
- {
- Console.Write("{0} ", resultMatrix[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- //////////////////////// triangle of pascal ////////////////////////////////
- private static int height;
- private static long[][] triangle;
- private static void Main()
- {
- height = 10;
- // Allocate the array in a triangle form
- triangle = new long[height + 1][];
- for (int row = 0; row <= height; row++)
- {
- triangle[row] = new long[row + 1];
- }
- // Calculate Triangle
- triangle[0][0] = 1;
- for (int row = 0; row < height; row++)
- {
- for (int col = 0; col <= row; col++)
- {
- triangle[row + 1][col] += triangle[row][col];
- triangle[row + 1][col + 1] += triangle[row][col];
- }
- }
- ///////////////////// // Print Triangle
- for (int row = 0; row <= height; row++)
- {
- Console.Write("".PadLeft((height - row) * 2));
- for (int col = 0; col <= row; col++)
- {
- Console.Write("{0,3} ", triangle[row][col]);
- }
- Console.WriteLine();
- }
- /////////////////////////////////////// 2 reshenie na triugulnika na paskal //////////////////////////////////////////////////////////
- private static int height;
- private static long[][] triangle;
- private static void Main()
- {
- height = 10;
- // Allocate the array in a triangle form
- triangle = new long[height + 1][];
- for (int row = 0; row <= height; row++)
- {
- triangle[row] = new long[row + 1];
- }
- // Calculate Triangle
- triangle[0][0] = 1;
- for (int row = 0; row < height; row++)
- {
- for (int col = 0; col <= row; col++)
- {
- triangle[row + 1][col] += triangle[row][col];
- triangle[row + 1][col + 1] += triangle[row][col];
- }
- }
- ///////////////////// // Print Triangle
- for (int row = 0; row <= height; row++)
- {
- Console.Write("".PadLeft((height - row) * 2));
- for (int col = 0; col <= row; col++)
- {
- Console.Write("{0,3} ", triangle[row][col]);
- }
- Console.WriteLine();
- }
- ////////////////////////////////////// MUTLI ARR HOMEWORKS //////////////////////////////////////////
- Problem 1. Fill the matrix /////////////////////////////////////////////////////
- static int[,] fillMatrixA(int n)
- {
- int startCount = 1;
- int[,] matrix = new int[n, n];
- for (int rows = 0; rows < n; rows++)
- {
- for (int cols = 0; cols < n; cols++)
- {
- matrix[cols, rows] = startCount++;
- }
- }
- return matrix;
- }
- static int[,] fillMatrixB(int n)
- {
- int[,] matrix = new int[n, n];
- int startCount = 1;
- for (int rows = 0; rows < n; rows++)
- {
- if (rows % 2 == 0)
- {
- for (int cols = 0; cols < n; cols++)
- {
- matrix[cols, rows] = startCount++;
- }
- }
- else
- {
- for (int cols = n - 1; cols >= 0; cols--)
- {
- matrix[cols, rows] = startCount++;
- }
- }
- }
- return matrix;
- }
- static int[,] fillMatrixC(int n)
- {
- int[,] matrix = new int[n, n];
- int rows = 0;
- int cols = 0;
- int startCount = 1;
- for (int i = n - 1; i >= 0; i--)
- {
- rows = i;
- cols = 0;
- while (rows < n && cols < n)
- {
- matrix[rows++, cols++] = startCount++;
- }
- }
- for (int j = 1; j < n; j++)
- {
- rows = j;
- cols = 0;
- while (rows < n && cols < n)
- {
- matrix[cols++, rows++] = startCount++;
- }
- }
- return matrix;
- }
- static int[,] fillMatrixD(int n)
- {
- int[,] result = new int[n, n];
- int numberToAdd = 1;
- int globalCounter = n;
- int tempValue = -n;
- int sum = -1;
- do
- {
- tempValue = -1 * tempValue / n;
- for (int i = 0; i < globalCounter; i++)
- {
- sum += tempValue;
- result[sum / n, sum % n] = numberToAdd;
- numberToAdd++;
- }
- tempValue *= n;
- globalCounter--;
- for (int i = 0; i < globalCounter; i++)
- {
- sum += tempValue;
- result[sum / n, sum % n] = numberToAdd;
- numberToAdd++;
- }
- } while (globalCounter > 0);
- return result;
- }
- static void PrintMatrix(int[,] matrix)
- {
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int p = 0; p < matrix.GetLength(1); p++)
- {
- Console.Write("{0, -4}", matrix[i, p]);
- }
- Console.WriteLine();
- }
- }
- static void Main()
- {
- Console.WriteLine("Please enter the n number:");
- int n = int.Parse(Console.ReadLine());
- Console.WriteLine("Please choose which matrix to print? Enter 1, 2, 3 or 4: ");
- int matrixChoice = int.Parse(Console.ReadLine());
- Console.WriteLine();
- if (matrixChoice == 1)
- {
- int[,] a = fillMatrixA(n);
- PrintMatrix(a);
- }
- if (matrixChoice == 2)
- {
- int[,] a = fillMatrixB(n);
- PrintMatrix(a);
- }
- if (matrixChoice == 3)
- {
- int[,] a = fillMatrixC(n);
- PrintMatrix(a);
- }
- if (matrixChoice == 4)
- {
- int[,] a = fillMatrixD(n);
- PrintMatrix(a);
- }
- else
- {
- Console.WriteLine("Error!");
- }
- }
- problem 2 max sum //////////////////////////////////////////////////////////////////////
- // Read the matrix dimensions
- Console.Write("Number of rows = ");
- int rows = int.Parse(Console.ReadLine());
- Console.Write("Number of columns = ");
- int cols = int.Parse(Console.ReadLine());
- // Allocate the matrix
- int[,] matrix = new int[rows, cols];
- // Enter the matrix elements
- FillTheMatrix(matrix, rows, cols);
- // Print the matrix on the console
- PrintMatrix(matrix, rows, cols);
- // Find the maximal sum platform of size 3 x 3
- int bestSum, bestRow, bestCol;
- FindMaxSumPlatform(out bestSum, matrix, out bestRow, out bestCol);
- // Print the result
- PrintResult(matrix, bestRow, bestCol, bestSum);
- }
- private static void PrintResult(int[,] matrix, int bestRow, int bestCol, int bestSum)
- {
- Console.WriteLine("\nThe best platform is:\n");
- Console.WriteLine("{0}\t{1}\t{2}",
- matrix[bestRow, bestCol],
- matrix[bestRow, bestCol + 1],
- matrix[bestRow, bestCol + 2]);
- Console.WriteLine("{0}\t{1}\t{2}",
- matrix[bestRow + 1, bestCol],
- matrix[bestRow + 1, bestCol + 1],
- matrix[bestRow + 1, bestCol + 2]);
- Console.WriteLine("{0}\t{1}\t{2}",
- matrix[bestRow + 2, bestCol],
- matrix[bestRow + 2, bestCol + 1],
- matrix[bestRow + 2, bestCol + 2]);
- Console.WriteLine("\nThe maximal sum is: {0}\n", bestSum);
- }
- private static void FindMaxSumPlatform(out int bestSum, int[,] matrix, out int bestRow, out int bestCol)
- {
- bestSum = int.MinValue;
- bestRow = 0;
- bestCol = 0;
- for (int row = 0; row < matrix.GetLength(0) - 2; row++)
- {
- for (int col = 0; col < matrix.GetLength(1) - 2; col++)
- {
- int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] +
- matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
- matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
- if (sum > bestSum)
- {
- bestSum = sum;
- bestRow = row;
- bestCol = col;
- }
- }
- }
- }
- private static void FillTheMatrix(int[,] matrix, int rows, int cols)
- {
- for (int row = 0; row < rows; row++)
- {
- for (int col = 0; col < cols; col++)
- {
- Console.Write("matrix[{0},{1}] = ", row, col);
- int element = int.Parse(Console.ReadLine());
- matrix[row, col] = element;
- }
- }
- }
- private static void PrintMatrix(int[,] matrix, int rows, int cols)
- {
- Console.WriteLine("\nThe matrix is as follows:\n");
- for (int row = 0; row < rows; row++)
- {
- for (int col = 0; col < cols; col++)
- {
- Console.Write("{0}\t", matrix[row, col]);
- }
- Console.WriteLine();
- }
- }
- Problem 3. Sequence n matrix //////////////////////////////////////////////////////////
- static string FindLongestSequence(string[,] m)
- {
- int result = 0;
- int max = 0;
- int currRow = 0;
- int currCol = 0;
- int currDiag = 0;
- int temp = 0;
- string maxStr = "";
- for (int rows = 0; rows < m.GetLength(0); rows++)
- {
- for (int cols = 0; cols < m.GetLength(1); cols++)
- {
- string curr = m[rows, cols];
- currRow = FindStreak(m, rows, cols, 0);
- currDiag = FindStreak(m, rows, cols, 1);
- currCol = FindStreak(m, rows, cols, 2);
- temp = Math.Max(Math.Max(currRow, currCol), currDiag);
- if (temp > max)
- {
- max = temp;
- maxStr = curr;
- }
- }
- if (max == m.GetLength(0))
- {
- return FormResult(max, maxStr);
- }
- }
- return FormResult(max, maxStr);
- }
- static string FormResult(int n, string str)
- {
- string res = "";
- for (int i = 0; i < n; i++)
- {
- res += str + ", ";
- }
- res = res.Substring(0, res.Length - 2);
- return res;
- }
- static int FindStreak(string[,] m, int startRow, int startCol, int direction)
- {
- int count = -1;
- //проверка на реда
- if (direction == 0)
- {
- count = 1;
- for (int i = startCol + 1; i < m.GetLength(1); i++)
- {
- if (m[startRow, i] == m[startRow, startCol])
- {
- count++;
- }
- }
- }
- //проверка на диагонала
- else if (direction == 1)
- {
- count = 1;
- int diagonalSize = (m.GetLength(0) < m.GetLength(1)) ? m.GetLength(0) : m.GetLength(1);
- for (int i = startCol + 1; i < diagonalSize; i++)
- {
- if (m[i, i] == m[startRow, startCol])
- {
- count++;
- }
- }
- }
- //проверка на колоната
- else if (direction == 2)
- {
- count = 1;
- for (int rows = startRow + 1; rows < m.GetLength(0); rows++)
- {
- if (m[rows, startCol] == m[startRow, startCol])
- {
- count++;
- }
- }
- }
- return count;
- }
- static void Main()
- {
- string[,] matrix = { {"ha", "fifi", "ho", "hi"},
- {"fo", "ha", "hi", "xx"},
- {"xxx", "ho", "ha", "xx"}};
- string[,] matrix2 = {{"s", "qq", "s"},
- {"pp", "pp", "s"},
- {"pp", "qq", "s"}};
- Console.WriteLine(FindLongestSequence(matrix));
- Console.WriteLine(FindLongestSequence(matrix2));
- }
- problem 4 binary search ///////////////////////////////////////////////////////////////////////////////////////////
- Console.Write("Enter a number N (size of array): ");
- int N = int.Parse(Console.ReadLine());
- Console.Write("Enter a number K: ");
- int K = int.Parse(Console.ReadLine());
- int[] numbers = new int[N];
- Console.WriteLine("\nEnter {0} number(s) to array: ", N);
- for (int i = 0; i < numbers.Length; i++)
- {
- Console.Write(" {0}: ", i + 1);
- numbers[i] = int.Parse(Console.ReadLine());
- }
- PrintLargestNumber(numbers, K);
- }
- // Prints largest number smaller or equal to 'k' using method Array.BinarySearch()
- static void PrintLargestNumber(int[] numbers, int k)
- {
- Array.Sort(numbers);
- int index = Array.BinarySearch(numbers, k);
- index = index >= 0 ? index : (index == -1 ? -1 : Math.Abs(index + 2));
- if (index != -1)
- {
- Console.WriteLine("\nFound smaller or equal number to K = {0}", k);
- Console.WriteLine("-> Result number: {0}\n", numbers[index]);
- }
- else
- {
- Console.WriteLine("\n- There is no smaller or equal to K = {0} number in array!\n", k);
- }
- }
- 6 problem 6 class matrix //////////////////////////////////////////////////////////////////////////////////////
- Matrix matrix1 = new Matrix(3, 3,
- 1, 2, 0,
- 0, 1, 1,
- 2, 0, 1);
- Matrix matrix2 = new Matrix(3, 3,
- 1, 1, 2,
- 2, 1, 1,
- 1, 2, 1);
- Console.WriteLine("First Matrix is:");
- Console.WriteLine(matrix1);
- Console.WriteLine("Second Matrix is:");
- Console.WriteLine(matrix2);
- Console.WriteLine("Addition of the Matrices:");
- Console.WriteLine(matrix1 + matrix2);
- Console.WriteLine("Subtraction of the Matrices:");
- Console.WriteLine(matrix1 - matrix2);
- Console.WriteLine("Multiplication of the Matrices:");
- Console.WriteLine(matrix1 * matrix2);
- }
- }
- class Matrix
- {
- // Fields
- private readonly int Rows;
- private readonly int Cols;
- private readonly int[,] matrix;
- // Constructors
- private Matrix(int rows, int cols)
- : this(rows, cols, new int[] { })
- {
- }
- public Matrix(int rows, int cols, params int[] numbers)
- {
- if (rows * cols != numbers.Length && numbers.Length != 0)
- {
- throw new ArgumentException();
- }
- matrix = new int[rows, cols];
- Rows = rows;
- Cols = cols;
- if (numbers.Length > 0)
- {
- Buffer.BlockCopy(numbers, 0, matrix, 0, rows * cols * sizeof(int));
- }
- }
- // Indexer for accessing the matrix content
- private int this[int row, int col]
- {
- get { return matrix[row, col]; }
- set { matrix[row, col] = value; }
- }
- // Override method ToString() to print appropriately matrix elements
- public override string ToString()
- {
- StringBuilder result = new StringBuilder();
- for (int row = 0; row < this.Rows; row++)
- {
- for (int col = 0; col < this.Cols; col++)
- {
- result.AppendFormat("{0,4}", matrix[row, col]);
- }
- result.AppendLine();
- }
- return result.ToString();
- }
- // Аddition (m1 + m2)
- public static Matrix operator +(Matrix matrix1, Matrix matrix2)
- {
- if (matrix1.Rows != matrix2.Rows || matrix1.Cols != matrix2.Cols)
- {
- Console.WriteLine("-> Invalid operation! Matrices must be of one and same type...");
- return null;
- }
- Matrix result = new Matrix(matrix1.Rows, matrix1.Cols);
- for (int row = 0; row < result.Rows; row++)
- {
- for (int col = 0; col < result.Cols; col++)
- {
- result[row, col] = matrix1[row, col] + matrix2[row, col];
- }
- }
- return result;
- }
- // Subtraction (m1 - m2)
- public static Matrix operator -(Matrix matrix1, Matrix matrix2)
- {
- if (matrix1.Rows != matrix2.Rows || matrix1.Cols != matrix2.Cols)
- {
- Console.WriteLine("-> Invalid operation! Matrices must be of one and same type...");
- return null;
- }
- Matrix result = new Matrix(matrix1.Rows, matrix1.Cols);
- for (int row = 0; row < result.Rows; row++)
- for (int col = 0; col < result.Cols; col++)
- result[row, col] = matrix1[row, col] - matrix2[row, col];
- return result;
- }
- // Multiplication (m1 * m2)
- public static Matrix operator *(Matrix matrix1, Matrix matrix2)
- {
- Matrix result = new Matrix(matrix1.Rows, matrix2.Cols);
- for (int row = 0; row < result.Rows; row++)
- {
- for (int col = 0; col < result.Cols; col++)
- {
- for (int k = 0; k < matrix1.Cols; k++) // or i < matrix2.Rows
- {
- result[row, col] += matrix1[row, k] * matrix2[k, col];
- }
- }
- }
- return result;
- }
- Problem 5. Sort by string length //////////////////////////////////////////////////////////////////
- Console.Write("Enter a number N (size of array): ");
- int N = int.Parse(Console.ReadLine());
- string[] elements = new string[N];
- Console.WriteLine("\nEnter {0} string(s) to array: ", N);
- for (int i = 0; i < elements.Length; i++)
- {
- Console.Write(" {0}: ", i + 1);
- elements[i] = Console.ReadLine();
- }
- Console.WriteLine("\nBefore sorting: {0}\n", string.Join(" ", elements));
- SelectionSortByLength(ref elements);
- Console.WriteLine("After sorting: {0}\n", string.Join(" ", elements));
- }
- static void SelectionSortByLength(ref string[] elements)
- {
- for (int i = 0; i < elements.Length - 1; i++)
- {
- int index = i;
- for (int j = i + 1; j < elements.Length; j++)
- {
- if (elements[j].Length < elements[index].Length) index = j;
- }
- string swap = elements[i];
- elements[i] = elements[index];
- elements[index] = swap;
- }
- }
- 7 problem 7 //////////////////////////////////////////////////////////////////////////////////////////
- class LargestAreaInMatrix
- {
- static int[,] matrix;
- static int bestLength = 0, bestNumber = 0;
- static int currentLength = 0, currentNumber = 0;
- static void Main()
- {
- matrix = new[,]
- {
- { 1, 3, 2, 2, 2, 4 },
- { 3, 3, 3, 2, 4, 4 },
- { 4, 3, 1, 2, 3, 3 },
- { 4, 3, 1, 3, 3, 1 },
- { 4, 3, 3, 3, 1, 1 }
- };
- PrintMatrix(matrix);
- FindBestAreaLength(matrix);
- }
- static void FindBestAreaLength(int[,] testMatrix)
- {
- bestLength = bestNumber = 0;
- for (int row = 0; row < testMatrix.GetLongLength(0); row++)
- {
- for (int col = 0; col < testMatrix.GetLongLength(1); col++)
- {
- currentNumber = testMatrix[row, col];
- currentLength = 0;
- GetAreaLength(row, col);
- if (currentLength > bestLength)
- {
- bestLength = currentLength;
- bestNumber = currentNumber;
- }
- }
- }
- Console.WriteLine("Best Area of number {0} -> {1} times\n", bestNumber, bestLength);
- }
- static void GetAreaLength(int row, int col)
- {
- if (row < 0 || row >= matrix.GetLongLength(0) ||
- col < 0 || col >= matrix.GetLongLength(1) ||
- matrix[row, col] == 0) return;
- if (matrix[row, col] == currentNumber)
- {
- matrix[row, col] = 0;
- currentLength++;
- GetAreaLength(row - 1, col);
- GetAreaLength(row + 1, col);
- GetAreaLength(row, col - 1);
- GetAreaLength(row, col + 1);
- }
- }
- static void PrintMatrix(int[,] testMatrix)
- {
- Console.WriteLine("Matrix ({0}x{1}):\n", testMatrix.GetLongLength(0), testMatrix.GetLongLength(1));
- for (int row = 0; row < testMatrix.GetLongLength(0); row++)
- {
- for (int col = 0; col < testMatrix.GetLongLength(1); col++)
- {
- Console.Write("{0,3}", testMatrix[row, col]);
- }
- Console.WriteLine();
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment