n4wn4w

C# mnogomerni masivi

Apr 20th, 2015
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 31.31 KB | None | 0 0
  1.  //////////////  3 meren masiv  cube/kub  //////////////////////////////////////////////////////////////
  2.  
  3.  int [,,] cube = new int[5, 5, 5];
  4.  
  5.  
  6.                    for (int i = 0; i < 5; i++)
  7.                    {
  8.                        for (int j = 0; j < 5; j++)
  9.                        {
  10.                            for (int k = 0; k < 5; k++)
  11.                            {
  12.                                cube[i, j, k] = i + j + k;
  13.                            }
  14.                        }
  15.                    }
  16.  
  17.  
  18.  
  19.                    for (int i = 0; i < 5; i++)
  20.                    {
  21.                        for (int j = 0; j < 5; j++)
  22.                        {
  23.                            for (int k = 0; k < 5; k++)
  24.                            {
  25.                                Console.Write("{0,4}", cube[i,j,k]);
  26.                            }
  27.                            Console.WriteLine();
  28.                        }
  29.                        Console.WriteLine("----------------------------");
  30.                    }
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  //////////////  matrica  3 X 3 ////////////
  39. // Declare and initialize the matrix
  40.          
  41.   int[,] matrix =
  42.             {
  43.                 { 0, 2, 4, 0, 9, 5 },
  44.                 { 7, 1, 3, 3, 2, 1 },
  45.                 { 1, 3, 9, 8, 5, 6 },
  46.                 { 4, 6, 7, 9, 1, 0 }
  47.             };
  48.  
  49.             // Find the maximal sum platform of size 2 x 2
  50.             int bestSum = int.MinValue;
  51.             int bestRow = 0;
  52.             int bestCol = 0;
  53.             for (int row = 0; row < matrix.GetLength(0) - 2; row++)
  54.             {
  55.                 for (int col = 0; col < matrix.GetLength(1) - 2; col++)
  56.                 {
  57.                     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];
  58.                     if (sum > bestSum)
  59.                     {
  60.                         bestSum = sum;
  61.                         bestRow = row;
  62.                         bestCol = col;
  63.                     }
  64.                 }
  65.             }
  66.  
  67.             // Print the result
  68.             Console.WriteLine("The best platform is:");
  69.             Console.WriteLine("  {0} {1} {2}", matrix[bestRow, bestCol], matrix[bestRow, bestCol + 1], matrix[bestRow, bestCol + 2]);
  70.             Console.WriteLine("  {0} {1} {2}", matrix[bestRow + 1, bestCol], matrix[bestRow + 1, bestCol + 1], matrix[bestRow + 1, bestCol + 2]);
  71.             Console.WriteLine("  {0} {1} {2}", matrix[bestRow + 2, bestCol], matrix[bestRow + 2, bestCol + 1], matrix[bestRow + 2, bestCol + 2]);
  72.             Console.WriteLine("The maximal sum is: {0}", bestSum);
  73.  
  74.  
  75.  
  76.  
  77. /////////////////////////  abstrakta matrica ////////////////
  78.  
  79.  
  80.  // Declare and initialize the matrix
  81.  
  82.             int width = 3;
  83.             int height = 2;
  84.  
  85.  
  86.             int[,] matrix =
  87.             {
  88.                 { 0, 2, 4, 0, 9, 5 },
  89.                 { 7, 1, 3, 3, 2, 1 },
  90.                 { 1, 3, 9, 8, 5, 6 },
  91.                 { 4, 6, 7, 9, 1, 0 }
  92.             };
  93.  
  94.             // Find the maximal sum platform of size 2 x 2
  95.             int bestSum = int.MinValue;
  96.             int bestRow = 0;
  97.             int bestCol = 0;
  98.             for (int row = 0; row < matrix.GetLength(0) - height +1; row++)
  99.             {
  100.                 for (int col = 0; col < matrix.GetLength(1) - width + 1; col++)
  101.                 {
  102.                     int sum = 0;
  103.                     for (int platformRow = row; platformRow < row + height; platformRow++)
  104.                     {
  105.                         for (int platformCol = col; platformCol < col + width; platformCol++)
  106.                         {
  107.                             sum += matrix[platformRow, platformCol];
  108.                         }
  109.                     }
  110.                     if (sum > bestSum)
  111.                     {
  112.                         bestSum = sum;
  113.                         bestRow = row;
  114.                         bestCol = col;
  115.                     }
  116.                 }
  117.             }
  118.  
  119.             // Print the result
  120.             Console.WriteLine("The best platform is:");
  121.             for (int platformRow = bestRow; platformRow < bestRow + height ; platformRow++)
  122.             {
  123.                 for (int platformCol = bestCol; platformCol < bestCol + width; platformCol++)
  124.                 {
  125.                     Console.Write("{0} ", matrix[platformRow , platformCol]);
  126.                 }
  127.                 Console.WriteLine();
  128.             }
  129.            // Console.WriteLine("  {0} {1}", matrix[bestRow, bestCol], matrix[bestRow, bestCol + 1]);
  130.            // Console.WriteLine("  {0} {1}", matrix[bestRow + 1, bestCol], matrix[bestRow + 1, bestCol + 1]);
  131.             Console.WriteLine("The maximal sum is: {0}", bestSum);
  132.  
  133.  
  134.  
  135. ///////////////////////////////  2 reshenie na gornata zadacha  /////////////////////////////////////////////////////
  136.  
  137.  
  138.  
  139.  
  140.         int height = 3;
  141.         int width = 3;
  142.  
  143.                    // Declare and initialize the matrix
  144.         int[,] matrix =
  145.             {
  146.                 { 0, 2, 4, 0, 9, 5 },
  147.                 { 7, 1, 3, 3, 2, 1 },
  148.                 { 1, 3, 9, 8, 5, 6 },
  149.                 { 4, 6, 7, 9, 1, 0 }
  150.             };
  151.  
  152.         // Find the maximal sum platform of size 2 x 2
  153.         int bestSum = int.MinValue;
  154.         int bestRow = 0;
  155.         int bestCol = 0;
  156.         for (int row = 0; row < matrix.GetLength(0) - 2; row++)
  157.         {
  158.             for (int col = 0; col < matrix.GetLength(1) - 2; col++)
  159.             {
  160.                 int sum = matrix[row + 0, col +0]
  161.                     + matrix[row + 0, col + 1]
  162.                     + matrix[row + 1, col + 0]
  163.                     + matrix[row + 1, col + 1]
  164.                     + matrix[row + 2, col + 0]
  165.                     + matrix[row + 0, col + 2]
  166.                     + matrix[row + 1, col + 2]
  167.                     + matrix[row + 2, col + 1]
  168.                     + matrix[row + 2, col + 2]
  169.                     ;
  170.                 if (sum > bestSum)
  171.                 {
  172.                     bestSum = sum;
  173.                     bestRow = row;
  174.                     bestCol = col;
  175.                 }
  176.             }
  177.         }
  178.  
  179.         Console.WriteLine("The best platform is: "+ bestSum);
  180.         for (int platformRow = bestRow; platformRow < bestRow + height; platformRow++)
  181.         {
  182.             for (int platformCol = bestCol; platformCol < bestCol + width; platformCol++)
  183.             {
  184.                 Console.Write("{0} ", matrix[platformRow, platformCol]);
  185.             }
  186.             Console.WriteLine();
  187.         }
  188.  
  189.  
  190. /////////////////////    nazubeni masivi /////////////
  191.  
  192. int[] numbers = { 0, 1, 4, 113, 55, 3, 1, 2, 66, 557, 124, 2 };
  193.         int[] sizes = new int[3];
  194.         int[] offsets = new int[3];
  195.  
  196.         // Calculate the sizes for each reminder (0, 1 and 2)
  197.         foreach (var number in numbers)
  198.         {
  199.             int remainder = number % 3;
  200.             sizes[remainder]++;
  201.         }
  202.  
  203.         // Calculate the list of numbers for each reminder (0, 1 and 2)
  204.         int[][] numbersByRemainder = new int[3][] { new int[sizes[0]], new int[sizes[1]], new int[sizes[2]] };
  205.         foreach (var number in numbers)
  206.         {
  207.             int remainder = number % 3;
  208.             int index = offsets[remainder];
  209.             numbersByRemainder[remainder][index] = number;
  210.             offsets[remainder]++;
  211.         }
  212.  
  213.         // Print the result jagged array
  214.         for (int row = 0; row < numbersByRemainder.GetLength(0); row++)
  215.         {
  216.             foreach (var num in numbersByRemainder[row])
  217.             {
  218.                 Console.Write(num + " ");
  219.             }
  220.             Console.WriteLine();
  221.         }
  222.  
  223.  
  224. /////////////// nazubeni masivi abstraktno ///////////////////////////
  225.  
  226.  
  227.             int n = 4;
  228.             int[] numbers = { 0, 1, 4, 113, 55, 3, 1, 2, 66, 557, 124, 2 };
  229.             int[] sizes = new int[n];
  230.             int[] offsets = new int[n];
  231.  
  232.             // Calculate the sizes for each reminder (0, 1 and 2)
  233.             foreach (var number in numbers)
  234.             {
  235.                 int remainder = number % n;
  236.                 sizes[remainder]++;
  237.             }
  238.  
  239.             // Calculate the list of numbers for each reminder (0, 1 and 2)
  240.             int[][] numbersByRemainder = new int[n][];
  241.             for (int i = 0; i < n; i++)
  242.             {
  243.                 numbersByRemainder[i] = new int[sizes[i]];
  244.             }
  245.             foreach (var number in numbers)
  246.             {
  247.                 int remainder = number % n;
  248.                 int index = offsets[remainder];
  249.                 numbersByRemainder[remainder][index] = number;
  250.                 offsets[remainder]++;
  251.             }
  252.  
  253.             // Print the result jagged array
  254.             for (int row = 0; row < numbersByRemainder.GetLength(0); row++)
  255.             {
  256.                 foreach (var num in numbersByRemainder[row])
  257.                 {
  258.                     Console.Write(num + " ");
  259.                 }
  260.                 Console.WriteLine();
  261.             }
  262.  
  263.  
  264. /////////////////  umnojenie na matrici ///////////////////////
  265.  
  266.  
  267.  
  268.             int[,] firstMatrix = new int[4, 2] {
  269.             { 1, 2 },
  270.             { 3, 4 },
  271.             { 5, 6 },
  272.             { 7, 8 } };
  273.  
  274.             // Print the first matrix
  275.             for (int row = 0; row < firstMatrix.GetLength(0); row++)
  276.             {
  277.                 for (int col = 0; col < firstMatrix.GetLength(1); col++)
  278.                 {
  279.                     Console.Write("{0} ", firstMatrix[row, col]);
  280.                 }
  281.  
  282.                 Console.WriteLine();
  283.             }
  284.  
  285.             Console.WriteLine();
  286.  
  287.             int[,] secondMatrix = new int[2, 3] {
  288.             { 1, 2, 3 },
  289.             { 4, 5, 6 } };
  290.  
  291.             // Print the second matrix
  292.             for (int row = 0; row < secondMatrix.GetLength(0); row++)
  293.             {
  294.                 for (int col = 0; col < secondMatrix.GetLength(1); col++)
  295.                 {
  296.                     Console.Write("{0} ", secondMatrix[row, col]);
  297.                 }
  298.  
  299.                 Console.WriteLine();
  300.             }
  301.  
  302.             Console.WriteLine();
  303.  
  304.             // Multiply the first matrix with the second matrix
  305.             int cols1 = firstMatrix.GetLength(1);
  306.             int rows1 = firstMatrix.GetLength(0);
  307.             int cols2 = secondMatrix.GetLength(1);
  308.             int rows2 = secondMatrix.GetLength(0);
  309.  
  310.             if (cols1 != rows2)
  311.             {
  312.                 throw new ArgumentException("Invalid dimensions!");
  313.             }
  314.  
  315.             int[,] resultMatrix = new int[rows1, cols2];
  316.             for (int row = 0; row < rows1; row++)
  317.             {
  318.                 for (int col = 0; col < cols2; col++)
  319.                 {
  320.                     resultMatrix[row, col] = 0;
  321.                     for (int i = 0; i < cols1; i++)
  322.                     {
  323.                         resultMatrix[row, col] += firstMatrix[row, i] * secondMatrix[i, col];
  324.                     }
  325.                 }
  326.             }
  327.  
  328.             // Print the result matrix
  329.             for (int row = 0; row < resultMatrix.GetLength(0); row++)
  330.             {
  331.                 for (int col = 0; col < resultMatrix.GetLength(1); col++)
  332.                 {
  333.                     Console.Write("{0} ", resultMatrix[row, col]);
  334.                 }
  335.  
  336.                 Console.WriteLine();
  337.             }
  338.  
  339.             Console.WriteLine();
  340.  
  341.  
  342. ////////////////////////  triangle of pascal  ////////////////////////////////
  343.  
  344.  
  345.     private static int height;
  346.  
  347.     private static long[][] triangle;
  348.  
  349.     private static void Main()
  350.     {
  351.         height = 10;
  352.  
  353.         // Allocate the array in a triangle form
  354.         triangle = new long[height + 1][];
  355.  
  356.         for (int row = 0; row <= height; row++)
  357.         {
  358.             triangle[row] = new long[row + 1];
  359.         }
  360.  
  361.         // Calculate Triangle
  362.         triangle[0][0] = 1;
  363.  
  364.         for (int row = 0; row < height; row++)
  365.         {
  366.             for (int col = 0; col <= row; col++)
  367.             {
  368.                 triangle[row + 1][col] += triangle[row][col];
  369.                 triangle[row + 1][col + 1] += triangle[row][col];
  370.             }
  371.         }
  372.  
  373. /////////////////////    // Print Triangle
  374.         for (int row = 0; row <= height; row++)
  375.         {
  376.             Console.Write("".PadLeft((height - row) * 2));
  377.             for (int col = 0; col <= row; col++)
  378.             {
  379.                 Console.Write("{0,3} ", triangle[row][col]);
  380.             }
  381.  
  382.             Console.WriteLine();
  383.         }
  384.  
  385.  
  386.  
  387. /////////////////////////////////////// 2 reshenie na triugulnika na paskal //////////////////////////////////////////////////////////
  388.  
  389.  
  390.  
  391.  private static int height;
  392.  
  393.     private static long[][] triangle;
  394.  
  395.     private static void Main()
  396.     {
  397.         height = 10;
  398.  
  399.         // Allocate the array in a triangle form
  400.         triangle = new long[height + 1][];
  401.  
  402.         for (int row = 0; row <= height; row++)
  403.         {
  404.             triangle[row] = new long[row + 1];
  405.         }
  406.  
  407.         // Calculate Triangle
  408.         triangle[0][0] = 1;
  409.  
  410.         for (int row = 0; row < height; row++)
  411.         {
  412.             for (int col = 0; col <= row; col++)
  413.             {
  414.                 triangle[row + 1][col] += triangle[row][col];
  415.                 triangle[row + 1][col + 1] += triangle[row][col];
  416.             }
  417.         }
  418.  
  419. /////////////////////    // Print Triangle
  420.         for (int row = 0; row <= height; row++)
  421.         {
  422.             Console.Write("".PadLeft((height - row) * 2));
  423.             for (int col = 0; col <= row; col++)
  424.             {
  425.                 Console.Write("{0,3} ", triangle[row][col]);
  426.             }
  427.  
  428.             Console.WriteLine();
  429.         }
  430.  
  431.  
  432.  
  433.  
  434.  
  435. //////////////////////////////////////       MUTLI ARR HOMEWORKS  //////////////////////////////////////////
  436.  
  437.    
  438.  
  439. Problem 1. Fill the matrix   /////////////////////////////////////////////////////
  440.  
  441. static int[,] fillMatrixA(int n)
  442.         {
  443.             int startCount = 1;
  444.             int[,] matrix = new int[n, n];
  445.             for (int rows = 0; rows < n; rows++)
  446.             {
  447.                 for (int cols = 0; cols < n; cols++)
  448.                 {
  449.                     matrix[cols, rows] = startCount++;
  450.                 }
  451.             }
  452.             return matrix;
  453.         }
  454.         static int[,] fillMatrixB(int n)
  455.         {
  456.             int[,] matrix = new int[n, n];
  457.             int startCount = 1;
  458.             for (int rows = 0; rows < n; rows++)
  459.             {
  460.                 if (rows % 2 == 0)
  461.                 {
  462.                     for (int cols = 0; cols < n; cols++)
  463.                     {
  464.                         matrix[cols, rows] = startCount++;
  465.                     }
  466.                 }
  467.                 else
  468.                 {
  469.                     for (int cols = n - 1; cols >= 0; cols--)
  470.                     {
  471.                         matrix[cols, rows] = startCount++;
  472.                     }
  473.                 }
  474.             }
  475.             return matrix;
  476.         }
  477.  
  478.         static int[,] fillMatrixC(int n)
  479.         {
  480.             int[,] matrix = new int[n, n];
  481.             int rows = 0;
  482.             int cols = 0;
  483.             int startCount = 1;
  484.  
  485.             for (int i = n - 1; i >= 0; i--)
  486.             {
  487.                 rows = i;
  488.                 cols = 0;
  489.                 while (rows < n && cols < n)
  490.                 {
  491.                     matrix[rows++, cols++] = startCount++;
  492.                 }
  493.             }
  494.  
  495.             for (int j = 1; j < n; j++)
  496.             {
  497.                 rows = j;
  498.                 cols = 0;
  499.                 while (rows < n && cols < n)
  500.                 {
  501.                     matrix[cols++, rows++] = startCount++;
  502.                 }
  503.             }
  504.             return matrix;
  505.         }
  506.         static int[,] fillMatrixD(int n)
  507.         {
  508.             int[,] result = new int[n, n];
  509.             int numberToAdd = 1;
  510.             int globalCounter = n;
  511.             int tempValue = -n;
  512.             int sum = -1;
  513.  
  514.             do
  515.             {
  516.                 tempValue = -1 * tempValue / n;
  517.                 for (int i = 0; i < globalCounter; i++)
  518.                 {
  519.                     sum += tempValue;
  520.                     result[sum / n, sum % n] = numberToAdd;
  521.                     numberToAdd++;
  522.                 }
  523.                 tempValue *= n;
  524.                 globalCounter--;
  525.                 for (int i = 0; i < globalCounter; i++)
  526.                 {
  527.                     sum += tempValue;
  528.                     result[sum / n, sum % n] = numberToAdd;
  529.                     numberToAdd++;
  530.                 }
  531.             } while (globalCounter > 0);
  532.  
  533.             return result;
  534.         }
  535.  
  536.         static void PrintMatrix(int[,] matrix)
  537.         {
  538.             for (int i = 0; i < matrix.GetLength(0); i++)
  539.             {
  540.                 for (int p = 0; p < matrix.GetLength(1); p++)
  541.                 {
  542.                     Console.Write("{0, -4}", matrix[i, p]);
  543.                 }
  544.                 Console.WriteLine();
  545.             }
  546.         }
  547.  
  548.         static void Main()
  549.         {
  550.             Console.WriteLine("Please enter the n number:");
  551.             int n = int.Parse(Console.ReadLine());
  552.  
  553.             Console.WriteLine("Please choose which matrix to print? Enter 1, 2, 3 or 4: ");
  554.             int matrixChoice = int.Parse(Console.ReadLine());
  555.             Console.WriteLine();
  556.  
  557.             if (matrixChoice == 1)
  558.             {
  559.                 int[,] a = fillMatrixA(n);
  560.                 PrintMatrix(a);
  561.             }
  562.  
  563.             if (matrixChoice == 2)
  564.             {
  565.                 int[,] a = fillMatrixB(n);
  566.                 PrintMatrix(a);
  567.             }
  568.  
  569.             if (matrixChoice == 3)
  570.             {
  571.                 int[,] a = fillMatrixC(n);
  572.                 PrintMatrix(a);
  573.             }
  574.  
  575.             if (matrixChoice == 4)
  576.             {
  577.                 int[,] a = fillMatrixD(n);
  578.                 PrintMatrix(a);
  579.             }
  580.             else
  581.             {
  582.                 Console.WriteLine("Error!");
  583.             }
  584.         }
  585.  
  586.  
  587.  
  588. problem 2   max sum //////////////////////////////////////////////////////////////////////
  589.  
  590.  
  591. // Read the matrix dimensions
  592.         Console.Write("Number of rows = ");
  593.         int rows = int.Parse(Console.ReadLine());
  594.         Console.Write("Number of columns = ");
  595.         int cols = int.Parse(Console.ReadLine());
  596.  
  597.         // Allocate the matrix
  598.         int[,] matrix = new int[rows, cols];
  599.  
  600.         // Enter the matrix elements
  601.         FillTheMatrix(matrix, rows, cols);
  602.  
  603.         // Print the matrix on the console
  604.         PrintMatrix(matrix, rows, cols);
  605.  
  606.         // Find the maximal sum platform of size 3 x 3
  607.         int bestSum, bestRow, bestCol;
  608.         FindMaxSumPlatform(out bestSum, matrix, out bestRow, out bestCol);
  609.  
  610.         // Print the result
  611.         PrintResult(matrix, bestRow, bestCol, bestSum);
  612.     }
  613.  
  614.     private static void PrintResult(int[,] matrix, int bestRow, int bestCol, int bestSum)
  615.     {
  616.         Console.WriteLine("\nThe best platform is:\n");
  617.         Console.WriteLine("{0}\t{1}\t{2}",
  618.             matrix[bestRow, bestCol],
  619.             matrix[bestRow, bestCol + 1],
  620.             matrix[bestRow, bestCol + 2]);
  621.         Console.WriteLine("{0}\t{1}\t{2}",
  622.             matrix[bestRow + 1, bestCol],
  623.             matrix[bestRow + 1, bestCol + 1],
  624.             matrix[bestRow + 1, bestCol + 2]);
  625.         Console.WriteLine("{0}\t{1}\t{2}",
  626.             matrix[bestRow + 2, bestCol],
  627.             matrix[bestRow + 2, bestCol + 1],
  628.             matrix[bestRow + 2, bestCol + 2]);
  629.         Console.WriteLine("\nThe maximal sum is: {0}\n", bestSum);
  630.     }
  631.  
  632.     private static void FindMaxSumPlatform(out int bestSum, int[,] matrix, out int bestRow, out int bestCol)
  633.     {
  634.         bestSum = int.MinValue;
  635.         bestRow = 0;
  636.         bestCol = 0;
  637.         for (int row = 0; row < matrix.GetLength(0) - 2; row++)
  638.         {
  639.             for (int col = 0; col < matrix.GetLength(1) - 2; col++)
  640.             {
  641.                 int sum = matrix[row, col] + matrix[row, col + 1] + matrix[row, col + 2] +
  642.                           matrix[row + 1, col] + matrix[row + 1, col + 1] + matrix[row + 1, col + 2] +
  643.                           matrix[row + 2, col] + matrix[row + 2, col + 1] + matrix[row + 2, col + 2];
  644.                 if (sum > bestSum)
  645.                 {
  646.                     bestSum = sum;
  647.                     bestRow = row;
  648.                     bestCol = col;
  649.                 }
  650.             }
  651.         }
  652.     }
  653.  
  654.     private static void FillTheMatrix(int[,] matrix, int rows, int cols)
  655.     {
  656.         for (int row = 0; row < rows; row++)
  657.         {
  658.             for (int col = 0; col < cols; col++)
  659.             {
  660.                 Console.Write("matrix[{0},{1}] = ", row, col);
  661.                 int element = int.Parse(Console.ReadLine());
  662.                 matrix[row, col] = element;
  663.             }
  664.         }
  665.     }
  666.  
  667.     private static void PrintMatrix(int[,] matrix, int rows, int cols)
  668.     {
  669.         Console.WriteLine("\nThe matrix is as follows:\n");
  670.         for (int row = 0; row < rows; row++)
  671.         {
  672.             for (int col = 0; col < cols; col++)
  673.             {
  674.                 Console.Write("{0}\t", matrix[row, col]);
  675.             }
  676.             Console.WriteLine();
  677.         }
  678.     }
  679.  
  680.  
  681.  
  682.  
  683.  
  684.  
  685.  
  686. Problem 3. Sequence n matrix  //////////////////////////////////////////////////////////
  687.  
  688.  
  689.    static string FindLongestSequence(string[,] m)
  690.         {
  691.             int result = 0;
  692.             int max = 0;
  693.             int currRow = 0;
  694.             int currCol = 0;
  695.             int currDiag = 0;
  696.             int temp = 0;
  697.             string maxStr = "";
  698.             for (int rows = 0; rows < m.GetLength(0); rows++)
  699.             {
  700.                 for (int cols = 0; cols < m.GetLength(1); cols++)
  701.                 {
  702.                     string curr = m[rows, cols];
  703.                     currRow = FindStreak(m, rows, cols, 0);
  704.                     currDiag = FindStreak(m, rows, cols, 1);
  705.                     currCol = FindStreak(m, rows, cols, 2);
  706.                     temp = Math.Max(Math.Max(currRow, currCol), currDiag);
  707.                     if (temp > max)
  708.                     {
  709.                         max = temp;
  710.                         maxStr = curr;
  711.                     }
  712.                 }
  713.                 if (max == m.GetLength(0))
  714.                 {
  715.                     return FormResult(max, maxStr);
  716.                 }
  717.             }
  718.             return FormResult(max, maxStr);
  719.         }
  720.         static string FormResult(int n, string str)
  721.         {
  722.             string res = "";
  723.             for (int i = 0; i < n; i++)
  724.             {
  725.                 res += str + ", ";
  726.             }
  727.             res = res.Substring(0, res.Length - 2);
  728.             return res;
  729.         }
  730.         static int FindStreak(string[,] m, int startRow, int startCol, int direction)
  731.         {
  732.             int count = -1;
  733.             //проверка на реда
  734.             if (direction == 0)
  735.             {
  736.                 count = 1;
  737.                 for (int i = startCol + 1; i < m.GetLength(1); i++)
  738.                 {
  739.                     if (m[startRow, i] == m[startRow, startCol])
  740.                     {
  741.                         count++;
  742.                     }
  743.                 }
  744.             }
  745.             //проверка на диагонала
  746.             else if (direction == 1)
  747.             {
  748.                 count = 1;
  749.                 int diagonalSize = (m.GetLength(0) < m.GetLength(1)) ? m.GetLength(0) : m.GetLength(1);
  750.                 for (int i = startCol + 1; i < diagonalSize; i++)
  751.                 {
  752.                     if (m[i, i] == m[startRow, startCol])
  753.                     {
  754.                         count++;
  755.                     }
  756.                 }
  757.             }
  758.             //проверка на колоната
  759.             else if (direction == 2)
  760.             {
  761.                 count = 1;
  762.                 for (int rows = startRow + 1; rows < m.GetLength(0); rows++)
  763.                 {
  764.                     if (m[rows, startCol] == m[startRow, startCol])
  765.                     {
  766.                         count++;
  767.                     }
  768.                 }
  769.             }
  770.             return count;
  771.         }
  772.         static void Main()
  773.         {
  774.             string[,] matrix = {  {"ha", "fifi", "ho", "hi"},
  775.                                   {"fo", "ha", "hi", "xx"},
  776.                                   {"xxx", "ho", "ha", "xx"}};
  777.             string[,] matrix2 = {{"s", "qq", "s"},
  778.                                  {"pp", "pp", "s"},
  779.                                  {"pp", "qq", "s"}};
  780.  
  781.             Console.WriteLine(FindLongestSequence(matrix));
  782.             Console.WriteLine(FindLongestSequence(matrix2));
  783.         }
  784.  
  785.  
  786. problem 4  binary search  ///////////////////////////////////////////////////////////////////////////////////////////
  787.  
  788.  
  789. Console.Write("Enter a number N (size of array): ");
  790.         int N = int.Parse(Console.ReadLine());
  791.  
  792.         Console.Write("Enter a number K: ");
  793.         int K = int.Parse(Console.ReadLine());
  794.  
  795.         int[] numbers = new int[N];
  796.         Console.WriteLine("\nEnter {0} number(s) to array: ", N);
  797.         for (int i = 0; i < numbers.Length; i++)
  798.         {
  799.             Console.Write("   {0}: ", i + 1);
  800.             numbers[i] = int.Parse(Console.ReadLine());
  801.         }
  802.  
  803.         PrintLargestNumber(numbers, K);
  804.     }
  805.  
  806.     // Prints largest number smaller or equal to 'k' using method Array.BinarySearch()
  807.     static void PrintLargestNumber(int[] numbers, int k)
  808.     {
  809.         Array.Sort(numbers);
  810.  
  811.         int index = Array.BinarySearch(numbers, k);
  812.         index = index >= 0 ? index : (index == -1 ? -1 : Math.Abs(index + 2));
  813.  
  814.         if (index != -1)
  815.         {
  816.             Console.WriteLine("\nFound smaller or equal number to K = {0}", k);
  817.             Console.WriteLine("-> Result number: {0}\n", numbers[index]);
  818.         }
  819.         else
  820.         {
  821.             Console.WriteLine("\n- There is no smaller or equal to K = {0} number in array!\n", k);
  822.         }
  823.     }
  824.  
  825.  
  826.  
  827. 6 problem 6 class matrix //////////////////////////////////////////////////////////////////////////////////////
  828.  
  829. Matrix matrix1 = new Matrix(3, 3,
  830.             1, 2, 0,
  831.             0, 1, 1,
  832.             2, 0, 1);
  833.  
  834.         Matrix matrix2 = new Matrix(3, 3,
  835.             1, 1, 2,
  836.             2, 1, 1,
  837.             1, 2, 1);
  838.  
  839.         Console.WriteLine("First Matrix is:");
  840.         Console.WriteLine(matrix1);
  841.  
  842.         Console.WriteLine("Second Matrix is:");
  843.         Console.WriteLine(matrix2);
  844.  
  845.         Console.WriteLine("Addition of the Matrices:");
  846.         Console.WriteLine(matrix1 + matrix2);
  847.  
  848.         Console.WriteLine("Subtraction of the Matrices:");
  849.         Console.WriteLine(matrix1 - matrix2);
  850.  
  851.         Console.WriteLine("Multiplication of the Matrices:");
  852.         Console.WriteLine(matrix1 * matrix2);
  853.     }
  854. }
  855.  
  856. class Matrix
  857. {
  858.     // Fields
  859.     private readonly int Rows;
  860.     private readonly int Cols;
  861.     private readonly int[,] matrix;
  862.  
  863.     // Constructors
  864.     private Matrix(int rows, int cols)
  865.         : this(rows, cols, new int[] { })
  866.     {
  867.     }
  868.  
  869.     public Matrix(int rows, int cols, params int[] numbers)
  870.     {
  871.         if (rows * cols != numbers.Length && numbers.Length != 0)
  872.         {
  873.             throw new ArgumentException();
  874.         }
  875.  
  876.         matrix = new int[rows, cols];
  877.         Rows = rows;
  878.         Cols = cols;
  879.  
  880.         if (numbers.Length > 0)
  881.         {
  882.             Buffer.BlockCopy(numbers, 0, matrix, 0, rows * cols * sizeof(int));
  883.         }
  884.     }
  885.  
  886.     // Indexer for accessing the matrix content
  887.     private int this[int row, int col]
  888.     {
  889.         get { return matrix[row, col]; }
  890.         set { matrix[row, col] = value; }
  891.     }
  892.  
  893.     // Override method ToString() to print appropriately matrix elements
  894.     public override string ToString()
  895.     {
  896.         StringBuilder result = new StringBuilder();
  897.  
  898.         for (int row = 0; row < this.Rows; row++)
  899.         {
  900.             for (int col = 0; col < this.Cols; col++)
  901.             {
  902.                 result.AppendFormat("{0,4}", matrix[row, col]);
  903.             }
  904.             result.AppendLine();
  905.         }
  906.  
  907.         return result.ToString();
  908.     }
  909.  
  910.     // Аddition (m1 + m2)
  911.     public static Matrix operator +(Matrix matrix1, Matrix matrix2)
  912.     {
  913.         if (matrix1.Rows != matrix2.Rows || matrix1.Cols != matrix2.Cols)
  914.         {
  915.             Console.WriteLine("-> Invalid operation! Matrices must be of one and same type...");
  916.             return null;
  917.         }
  918.  
  919.         Matrix result = new Matrix(matrix1.Rows, matrix1.Cols);
  920.  
  921.         for (int row = 0; row < result.Rows; row++)
  922.         {
  923.             for (int col = 0; col < result.Cols; col++)
  924.             {
  925.                 result[row, col] = matrix1[row, col] + matrix2[row, col];
  926.             }
  927.         }
  928.  
  929.         return result;
  930.     }
  931.  
  932.     // Subtraction (m1 - m2)
  933.     public static Matrix operator -(Matrix matrix1, Matrix matrix2)
  934.     {
  935.         if (matrix1.Rows != matrix2.Rows || matrix1.Cols != matrix2.Cols)
  936.         {
  937.             Console.WriteLine("-> Invalid operation! Matrices must be of one and same type...");
  938.             return null;
  939.         }
  940.  
  941.         Matrix result = new Matrix(matrix1.Rows, matrix1.Cols);
  942.  
  943.         for (int row = 0; row < result.Rows; row++)
  944.             for (int col = 0; col < result.Cols; col++)
  945.                 result[row, col] = matrix1[row, col] - matrix2[row, col];
  946.  
  947.         return result;
  948.     }
  949.  
  950.     // Multiplication (m1 * m2)
  951.     public static Matrix operator *(Matrix matrix1, Matrix matrix2)
  952.     {
  953.         Matrix result = new Matrix(matrix1.Rows, matrix2.Cols);
  954.  
  955.         for (int row = 0; row < result.Rows; row++)
  956.         {
  957.             for (int col = 0; col < result.Cols; col++)
  958.             {
  959.                 for (int k = 0; k < matrix1.Cols; k++) // or i < matrix2.Rows
  960.                 {
  961.                     result[row, col] += matrix1[row, k] * matrix2[k, col];
  962.                 }
  963.             }
  964.         }
  965.  
  966.         return result;
  967.     }
  968.  
  969.  
  970. Problem 5. Sort by string length   //////////////////////////////////////////////////////////////////
  971.  
  972. Console.Write("Enter a number N (size of array): ");
  973.         int N = int.Parse(Console.ReadLine());
  974.  
  975.         string[] elements = new string[N];
  976.         Console.WriteLine("\nEnter {0} string(s) to array: ", N);
  977.  
  978.         for (int i = 0; i < elements.Length; i++)
  979.         {
  980.             Console.Write("   {0}: ", i + 1);
  981.             elements[i] = Console.ReadLine();
  982.         }
  983.  
  984.         Console.WriteLine("\nBefore sorting: {0}\n", string.Join(" ", elements));
  985.  
  986.         SelectionSortByLength(ref elements);
  987.  
  988.         Console.WriteLine("After sorting: {0}\n", string.Join(" ", elements));
  989.     }
  990.  
  991.     static void SelectionSortByLength(ref string[] elements)
  992.     {
  993.         for (int i = 0; i < elements.Length - 1; i++)
  994.         {
  995.             int index = i;
  996.  
  997.             for (int j = i + 1; j < elements.Length; j++)
  998.             {
  999.                 if (elements[j].Length < elements[index].Length) index = j;
  1000.             }
  1001.  
  1002.             string swap = elements[i];
  1003.             elements[i] = elements[index];
  1004.             elements[index] = swap;
  1005.         }
  1006.     }
  1007.  
  1008.  
  1009.  
  1010. 7 problem 7   //////////////////////////////////////////////////////////////////////////////////////////
  1011.  
  1012.  
  1013.  
  1014. class LargestAreaInMatrix
  1015. {
  1016.     static int[,] matrix;
  1017.     static int bestLength = 0, bestNumber = 0;
  1018.     static int currentLength = 0, currentNumber = 0;
  1019.     static void Main()
  1020.     {
  1021.         matrix = new[,]
  1022.         {
  1023.             { 1, 3, 2, 2, 2, 4 },
  1024.             { 3, 3, 3, 2, 4, 4 },
  1025.             { 4, 3, 1, 2, 3, 3 },
  1026.             { 4, 3, 1, 3, 3, 1 },
  1027.             { 4, 3, 3, 3, 1, 1 }
  1028.         };
  1029.  
  1030.         PrintMatrix(matrix);
  1031.         FindBestAreaLength(matrix);
  1032.     }
  1033.  
  1034.     static void FindBestAreaLength(int[,] testMatrix)
  1035.     {
  1036.         bestLength = bestNumber = 0;
  1037.  
  1038.         for (int row = 0; row < testMatrix.GetLongLength(0); row++)
  1039.         {
  1040.             for (int col = 0; col < testMatrix.GetLongLength(1); col++)
  1041.             {
  1042.                 currentNumber = testMatrix[row, col];
  1043.                 currentLength = 0;
  1044.  
  1045.                 GetAreaLength(row, col);
  1046.  
  1047.                 if (currentLength > bestLength)
  1048.                 {
  1049.                     bestLength = currentLength;
  1050.                     bestNumber = currentNumber;
  1051.                 }
  1052.             }
  1053.         }
  1054.  
  1055.         Console.WriteLine("Best Area of number {0} -> {1} times\n", bestNumber, bestLength);
  1056.     }
  1057.  
  1058.     static void GetAreaLength(int row, int col)
  1059.     {
  1060.         if (row < 0 || row >= matrix.GetLongLength(0) ||
  1061.             col < 0 || col >= matrix.GetLongLength(1) ||
  1062.             matrix[row, col] == 0) return;
  1063.  
  1064.         if (matrix[row, col] == currentNumber)
  1065.         {
  1066.             matrix[row, col] = 0;
  1067.             currentLength++;
  1068.  
  1069.             GetAreaLength(row - 1, col);
  1070.             GetAreaLength(row + 1, col);
  1071.             GetAreaLength(row, col - 1);
  1072.             GetAreaLength(row, col + 1);
  1073.         }
  1074.     }
  1075.  
  1076.     static void PrintMatrix(int[,] testMatrix)
  1077.     {
  1078.         Console.WriteLine("Matrix ({0}x{1}):\n", testMatrix.GetLongLength(0), testMatrix.GetLongLength(1));
  1079.         for (int row = 0; row < testMatrix.GetLongLength(0); row++)
  1080.         {
  1081.             for (int col = 0; col < testMatrix.GetLongLength(1); col++)
  1082.             {
  1083.                 Console.Write("{0,3}", testMatrix[row, col]);
  1084.             }
  1085.             Console.WriteLine();
  1086.         }
  1087.         Console.WriteLine();
  1088.     }
  1089. }
Advertisement
Add Comment
Please, Sign In to add comment