Anonim_999

Sashka

Nov 22nd, 2022
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.46 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace ConsoleApp23
  5. {
  6.     class Program
  7.     {
  8.         public static int[] A;
  9.         public static int CountElements;
  10.         public static int CountThreads;
  11.         public static int SubArrayLen;
  12.         public static int MaxOddNumber = 0;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             string userInput;
  17.             Console.Write("Количество элементов в массиве: ");
  18.             userInput = Console.ReadLine();
  19.  
  20.             if (int.TryParse(userInput,out CountElements) == false || CountElements < 1)
  21.             {
  22.                 CountElements = 10;
  23.                 Console.Write($"Количество элменетов заданы не правильно: {CountElements}");
  24.             }
  25.             A = new int[CountElements];
  26.             Console.WriteLine("Количество потоков: ");
  27.             userInput = Console.ReadLine();
  28.  
  29.             if (int.TryParse(userInput,out CountThreads) == false || CountThreads < 1)
  30.             {
  31.                 CountThreads = 1;
  32.                 Console.WriteLine($"Количество потоков заданы не правильно: {CountThreads}");
  33.             }
  34.  
  35.             if (CountThreads > CountElements)
  36.                 CountThreads = CountElements;
  37.             Console.WriteLine("Заполнение массива A:");
  38.  
  39.             for (int i = 0; i < CountElements; i++)
  40.             {
  41.                 Console.Write((i+1) + ": ");
  42.  
  43.                 if (int.TryParse(Console.ReadLine(),out A[i]) == false || A[i] < 1)
  44.                 {
  45.                     Console.WriteLine("Нерпавильный ввод: ");
  46.                     i--;
  47.                 }
  48.             }
  49.             Console.Clear();
  50.             Console.Write("Элементы массива: ");
  51.  
  52.             foreach (int item in A)
  53.                 Console.Write($"{item} ");
  54.             Console.WriteLine();
  55.             SubArrayLen = CountElements / CountThreads + 1;
  56.             Thread[] threads = new Thread[CountThreads];
  57.  
  58.             for (int i = 0; i < CountThreads; i++)
  59.             {
  60.                 PartArray partArray = new PartArray
  61.                 {
  62.                     StartIndex = i * SubArrayLen,
  63.                     EndIndex = (i + 1) * SubArrayLen
  64.                 };
  65.  
  66.                 if (partArray.EndIndex > CountElements)
  67.                     partArray.EndIndex = CountElements;
  68.                 threads[i] = new Thread(() => FindMaxOddNumber(partArray));
  69.                 threads[i].Name = "Поток№" + (i+1);
  70.                 threads[i].Start();
  71.             }
  72.  
  73.             foreach (Thread thread in threads)
  74.             {
  75.                 Console.WriteLine($"{thread.Name} - начал");
  76.                 thread.Join();
  77.                 Console.WriteLine($"{thread.Name} - закончил");
  78.             }
  79.             Console.WriteLine($"Максимальное нечетное число: {MaxOddNumber}");
  80.             Console.ReadLine();
  81.         }
  82.  
  83.         private static void FindMaxOddNumber(PartArray partArray)
  84.         {
  85.             int start = partArray.StartIndex;
  86.             int end = partArray.EndIndex;
  87.  
  88.             for (int i = start; i < end; i++)
  89.                 if (A[i] % 2 != 0)
  90.                     if (MaxOddNumber < A[i])
  91.                         MaxOddNumber = A[i];
  92.         }
  93.     }
  94.  
  95.     class PartArray
  96.     {
  97.         public int StartIndex;
  98.         public int EndIndex;
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment