Venciity

[Jumpstart C#] Multidimensional-Arrays 02-NegaitveElements

Mar 5th, 2014
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. //  Дадена е матрицата A(M,N). Намерете номера на стълба с най-мно­го от­ри­ца­тел­ни елементи.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace _02_NegaitveElements
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int[,] a = new int[4, 5]
  15.             {
  16.                 {5, 1, 6, -9, 4},
  17.                 {3, 7, -5, 0, -3},
  18.                 {-1, 4, -2, 6, 3},
  19.                 {-1, 4, -2, 6, 3}
  20.             };
  21.  
  22.             int rows = a.GetLength(0);
  23.             int cols = a.GetLength(1);
  24.  
  25.             int maxCol = 0;
  26.             int maxCount = 0;
  27.  
  28.             for (int i = 0; i < cols; i++)
  29.             {
  30.                 int count = 0;
  31.                 for (int j = 0; j < rows; j++)
  32.                 {
  33.                     if (a[j, i] < 0)
  34.                     {
  35.                         count++;
  36.                     }
  37.                 }
  38.  
  39.                 if (count > maxCount)
  40.                 {
  41.                     maxCount = count;
  42.                     maxCol = i;
  43.                 }
  44.             }
  45.  
  46.             Console.WriteLine("Column = {0}, count = {1}", maxCol, maxCount);
  47.         }
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment