Venciity

[Jumpstart C#] Multidimensional-Arrays 01-MaxAfterKCol

Mar 5th, 2014
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.41 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_MinBeforeKCol
  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 < cols - 1)
  30.             {
  31.                 int row = 0;
  32.                 int col = k+1;
  33.                 int max = a[row, col];
  34.  
  35.                 for (int i = k+1; i < cols; i++)
  36.                 {
  37.                     for (int j = 0; j < rows; j++)
  38.                     {
  39.                         if (max < a[j, i])
  40.                         {
  41.                             max = a[j, i];
  42.                             row = j;
  43.                             col = i;
  44.                         }
  45.                     }
  46.                 }
  47.                 Console.WriteLine("Max element at row #{0} and col #{1} is {2}", row, col, max);
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment