growhack

Task with local max

Mar 31st, 2020
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.19 KB | None | 0 0
  1. using System;
  2.  
  3. namespace LocalMax
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             const int elementsInArray = 30;
  10.             int[] array = new int[elementsInArray];
  11.             Random rand = new Random();
  12.             Console.WriteLine("Исходный массив: ");
  13.             if (array.Length == 1) Console.Write(array[0]);
  14.             for (int i = 0; i < array.Length; i++)
  15.             {
  16.                 array[i] = rand.Next(1, 100);
  17.                 Console.Write(array[i] + " ");
  18.             }
  19.             Console.Write("\nЛокальные максимумы: \n");
  20.             for (int i = 0; i < array.Length; i++)
  21.             {
  22.                 if (i == 0 && array[i] > array[i + 1])
  23.                 {
  24.                     Console.Write(array[i] + " ");
  25.                 }
  26.                 else if (i == array.Length - 1 && array[i] > array[i - 1])
  27.                 {
  28.                     Console.Write(array[i] + " ");
  29.                 }
  30.                 else if (array[i] > array[i - 1] && array[i] > array[i + 1])
  31.                 {
  32.                     Console.Write(array[i] + " ");
  33.                 }
  34.  
  35.             }
  36.         }
  37.     }
  38. }
Add Comment
Please, Sign In to add comment