Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- namespace ConsoleApp23
- {
- class Program
- {
- public static int[] A;
- public static int CountElements;
- public static int CountThreads;
- public static int SubArrayLen;
- public static int MaxOddNumber = 0;
- static void Main(string[] args)
- {
- string userInput;
- Console.Write("Количество элементов в массиве: ");
- userInput = Console.ReadLine();
- if (int.TryParse(userInput,out CountElements) == false || CountElements < 1)
- {
- CountElements = 10;
- Console.Write($"Количество элменетов заданы не правильно: {CountElements}");
- }
- A = new int[CountElements];
- Console.WriteLine("Количество потоков: ");
- userInput = Console.ReadLine();
- if (int.TryParse(userInput,out CountThreads) == false || CountThreads < 1)
- {
- CountThreads = 1;
- Console.WriteLine($"Количество потоков заданы не правильно: {CountThreads}");
- }
- if (CountThreads > CountElements)
- CountThreads = CountElements;
- Console.WriteLine("Заполнение массива A:");
- for (int i = 0; i < CountElements; i++)
- {
- Console.Write((i+1) + ": ");
- if (int.TryParse(Console.ReadLine(),out A[i]) == false || A[i] < 1)
- {
- Console.WriteLine("Нерпавильный ввод: ");
- i--;
- }
- }
- Console.Clear();
- Console.Write("Элементы массива: ");
- foreach (int item in A)
- Console.Write($"{item} ");
- Console.WriteLine();
- SubArrayLen = CountElements / CountThreads + 1;
- Thread[] threads = new Thread[CountThreads];
- for (int i = 0; i < CountThreads; i++)
- {
- PartArray partArray = new PartArray
- {
- StartIndex = i * SubArrayLen,
- EndIndex = (i + 1) * SubArrayLen
- };
- if (partArray.EndIndex > CountElements)
- partArray.EndIndex = CountElements;
- threads[i] = new Thread(() => FindMaxOddNumber(partArray));
- threads[i].Name = "Поток№" + (i+1);
- threads[i].Start();
- }
- foreach (Thread thread in threads)
- {
- Console.WriteLine($"{thread.Name} - начал");
- thread.Join();
- Console.WriteLine($"{thread.Name} - закончил");
- }
- Console.WriteLine($"Максимальное нечетное число: {MaxOddNumber}");
- Console.ReadLine();
- }
- private static void FindMaxOddNumber(PartArray partArray)
- {
- int start = partArray.StartIndex;
- int end = partArray.EndIndex;
- for (int i = start; i < end; i++)
- if (A[i] % 2 != 0)
- if (MaxOddNumber < A[i])
- MaxOddNumber = A[i];
- }
- }
- class PartArray
- {
- public int StartIndex;
- public int EndIndex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment