Venciity

[Jumpstart C#] Multidimensional-Arrays 01-MinUnderKRow

Mar 5th, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.40 KB | None | 0 0
  1. /* Дадена е матрицата A(M,N). При зададено k, намерете:
  2.  * най-малкия еле­мент под k-тия ред.   */
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _01_MinUnderKRow
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             Console.Write("Enter k: ");
  17.             int k = Int32.Parse(Console.ReadLine());
  18.  
  19.             int[,] a = new int[3, 5]
  20.             {
  21.                 {5, 1, 6, 9, 4},
  22.                 {3, 7, 5, 0, 3},
  23.                 {1, 4, 2, 6, 3}
  24.             };
  25.  
  26.             int rows = a.GetLength(0);
  27.             int cols = a.GetLength(1);
  28.  
  29.             if (k < rows - 1)
  30.             {
  31.                 int row = k+1;
  32.                 int col = 0;
  33.                 int min = a[k+1, 0];
  34.  
  35.                 for (int i = k + 1; i < rows; i++)
  36.                 {
  37.                     for (int j = 0; j < cols; j++)
  38.                     {
  39.                         if (min > a[i, j])
  40.                         {
  41.                             min = a[i, j];
  42.                             row = i;
  43.                             col = j;
  44.                         }
  45.                     }
  46.                 }
  47.                 Console.WriteLine("Min element at row #{0} and col #{1} is {2}", row, col, min);
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment