Vladislav_Bezruk

Ravenclaw_first_practice_task

Nov 20th, 2020 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.58 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Ravenclaw_first_practice_task
  4. {
  5.     class Program
  6.     {
  7.         static int Maximum(int Pos , int Count , int[] Num)
  8.         {
  9.             int MaxCount = 0, Max = -2147483647, PreMax = 0 , MMax = 0;
  10.  
  11.             while (!(MaxCount == Pos || MaxCount >= Count))
  12.             {
  13.                 Max = -2147483647;
  14.                 for (int i = 0; i < Count; i++)
  15.                     if (Num[i] > Max && (Num[i] < PreMax || MaxCount == 0))
  16.                         Max = Num[i];
  17.                 if (MaxCount == 0)
  18.                     MMax = Max;
  19.                 if (Max == -2147483647)
  20.                     break;
  21.                 PreMax = Max;
  22.                 MaxCount++;
  23.             }
  24.             if (MaxCount == Pos)
  25.                 return Max;
  26.             else
  27.                 return MMax;
  28.         }
  29.  
  30.         static void Main(string[] args)
  31.         {
  32.             int Count , Pos;
  33.  
  34.             Console.WriteLine("Enter the degree of maximum you want to find:");
  35.             Pos = Convert.ToInt32(Console.ReadLine());
  36.             Console.WriteLine("Enter the number of elements in the array:");
  37.             Count = Convert.ToInt32(Console.ReadLine());
  38.             int[] Num = new int[Count];
  39.  
  40.             for (int i = 0; i < Count; i++)
  41.             {
  42.                 Console.WriteLine("Enter the " + (i + 1) + " element in the array:");
  43.                 Num[i] = Convert.ToInt32(Console.ReadLine());
  44.             }
  45.  
  46.             Console.WriteLine("The " + Pos + " maximum = " + Maximum(Pos, Count, Num));
  47.             Console.ReadKey();
  48.         }
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment