rukvir

HW 4_2_1

Mar 16th, 2025
726
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. using System;
  2.  
  3. namespace HomeWork
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int numberItemsInArray = 10;
  10.             int[] userArray = new int[numberItemsInArray];
  11.             Random random = new Random();
  12.             int minNumbermInArray = 1;
  13.             int maxNumbermInArray = 70;
  14.  
  15.             for (int k = 0; k < numberItemsInArray; k++)
  16.             {
  17.                 userArray[k] = random.Next(minNumbermInArray, maxNumbermInArray);
  18.             }
  19.            
  20.             Console.WriteLine($"Исходный массив размером {numberItemsInArray} содержит:");
  21.            
  22.             for (int i = 0; i < userArray.Length; i++)
  23.             {
  24.                 Console.Write($"{userArray[i]} ");
  25.             }
  26.  
  27.             Console.WriteLine("\n----------------------------------");
  28.  
  29.             for (int j = 0; j < userArray.Length; j++)
  30.             {
  31.                 bool isLocalMax = true;
  32.  
  33.                 if (j == 0)
  34.                 {
  35.                     if (userArray[j] <= userArray[j + 1])
  36.                     {
  37.                         isLocalMax = false;
  38.                     }
  39.                 }
  40.                 else if (j == userArray.Length - 1)
  41.                 {
  42.                     if (userArray[j] <= userArray[j - 1])
  43.                     {
  44.                         isLocalMax = false;
  45.                     }
  46.                 }
  47.                 else
  48.                 {
  49.                     if (userArray[j] <= userArray[j - 1] || userArray[j] <= userArray[j + 1])
  50.                     {
  51.                         isLocalMax = false;
  52.                     }
  53.                 }
  54.  
  55.                 if (isLocalMax == true)
  56.                 {
  57.                     Console.WriteLine($"Локальный максимум: {userArray[j]}, позиция: {j}");
  58.                 }
  59.             }
  60.  
  61.             Console.ReadKey();
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment