Advertisement
Vlad_Savitskiy

Local max

Apr 8th, 2020
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.33 KB | None | 0 0
  1. using System;
  2.  
  3. namespace CSLightFirst
  4. {
  5.     class Program
  6.     {
  7.         static void Main()
  8.         {
  9.             Random rand = new Random();
  10.             int[] data = new int[30];
  11.  
  12.             for (int i = 0; i < data.Length; i++)
  13.                 data[i] = rand.Next(0, 9);
  14.  
  15.             Console.ForegroundColor = ConsoleColor.Blue;
  16.             Console.WriteLine($"Локальные максимумы массива из {data.Length} элем.:\n");
  17.             Console.ForegroundColor = ConsoleColor.White;
  18.  
  19.             if (data.Length == 0)
  20.                 return;
  21.             if (data.Length == 1)
  22.             {
  23.                 Console.WriteLine(data[0]);
  24.                 return;
  25.             }
  26.  
  27.             for (int i = 0; i < data.Length; i++)
  28.             {
  29.                 if (i == 0)
  30.                 {
  31.                     if (data[i] > data[i + 1])
  32.                         Console.Write(data[i] + " ");
  33.                     continue;
  34.                 }
  35.  
  36.                 if (i == data.Length - 1)
  37.                 {
  38.                     if (data[i] > data[i - 1])
  39.                         Console.Write(data[i] + " ");
  40.                     continue;
  41.                 }
  42.  
  43.                 if (data[i] > data[i - 1] && data[i] > data[i + 1])
  44.                     Console.Write(data[i] + " ");
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement