RMarK0

Untitled

Oct 24th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApp1
  8. {
  9.     class Program
  10.     {
  11.         static int FindNegativesInStroke(double[,] a, int i)
  12.         {
  13.             int negatives = 0;
  14.             for (int j = 0; j < a.GetLength(dimension: 1); j++)
  15.             {
  16.                 if (a[i, j] < 0)
  17.                     negatives++;
  18.             }
  19.  
  20.             return negatives;
  21.         }
  22.  
  23.         static void WriteStroke(double[,] a, int i)
  24.         {
  25.             for (int j = 0; j < a.GetLength(1); j++)
  26.                 Console.Write("{0,6}", a[i, j]);
  27.             Console.WriteLine();
  28.         }
  29.  
  30.         static void SortByNegatives(double[,] a)
  31.         {
  32.             Dictionary<int, int> arrayDictionary = new Dictionary<int, int>();
  33.  
  34.             for (int i = 0; i < a.GetLength(dimension: 0); i++)
  35.             {
  36.                 int negatives = FindNegativesInStroke(a, i);
  37.                 arrayDictionary.Add(i, negatives);
  38.             }
  39.             Console.WriteLine("Кол-во отриц. чисел в каждой строкe");
  40.             foreach (KeyValuePair<int,int> keyValue in arrayDictionary)
  41.             {
  42.                
  43.                 Console.WriteLine($"{keyValue.Key} - {keyValue.Value} чисел");
  44.             }
  45.  
  46.             var myList = arrayDictionary.ToList();
  47.  
  48.             myList.Sort((pair1, pair2) => pair1.Value.CompareTo(pair2.Value));
  49.             Console.WriteLine();
  50.             foreach (KeyValuePair<int, int> keyValue in myList)
  51.             {
  52.                 Console.WriteLine($"{keyValue.Key} - {keyValue.Value} чисел");
  53.             }
  54.  
  55.             Console.WriteLine("Новая матрица");
  56.             foreach (KeyValuePair<int, int> keyValue in myList)
  57.             {
  58.                 WriteStroke(a, keyValue.Key);
  59.             }
  60.         }
  61.  
  62.         static void CreateMatrix(ref double[,] a)
  63.         {
  64.             Random r = new Random();
  65.             Console.WriteLine("\nисходная матрица");
  66.             for (int i = 0; i < a.GetLength(dimension: 0); i++)
  67.             {
  68.                 for (int j = 0; j < a.GetLength(dimension: 1); j++)
  69.                 {
  70.                     a[i, j] = r.Next(-4, 5);
  71.                     Console.Write("{0,6}", a[i, j]);
  72.                 }
  73.                 Console.WriteLine();
  74.             }
  75.         }
  76.  
  77.         static void Main(string[] args)
  78.         {
  79.             // Расположить строки матрицы в порядке возрастания количества отрицательных элементов.
  80.             // Для нахождения количества отрицательных элементов строки использовать метод.
  81.             double[,] array = new double[5,6];
  82.             CreateMatrix(ref array);
  83.             SortByNegatives(array);
  84.            
  85.             Console.Read();
  86.         }
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment