Advertisement
Venciity

[Jumpstart C#] Arrays 05-MaxSamelNumbersInRange

Feb 17th, 2014
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. /* Напишете програма която намира и отпечатва максималната по дължина поредица от еднакви елементи в масив.
  2. Пример: { 2, 1, 1, 2, 3, 3, 2, 2, 2, 1 } -> { 2, 2, 2 }   */
  3.  
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9.  
  10. namespace _05_MaxSamelNumbersInRange
  11. {
  12.     class MaxSamelNumbersInRange
  13.     {
  14.         static void Main()
  15.         {
  16.             Console.Write("N = ");
  17.             int n = Int32.Parse(Console.ReadLine());
  18.             int[] array = new int[n];
  19.             for (int i = 0; i < n; i++)
  20.             {
  21.                 Console.Write("Element {0} = ", i+1);
  22.                 array[i] = Int32.Parse(Console.ReadLine());
  23.             }
  24.  
  25.             int maxLenght = 0;
  26.             int startPosition = 0;
  27.  
  28.             for (int i = 0; i < array.Length; i++)
  29.             {
  30.                 int j = i + 1;
  31.                 while (j < array.Length && array[i] == array[j])
  32.                 {
  33.                     j++;
  34.                 }
  35.  
  36.                 int length = j - i;
  37.                 if (length > maxLenght)
  38.                 {
  39.                     maxLenght = length;
  40.                     startPosition = i;
  41.                 }
  42.             }
  43.  
  44.             Console.WriteLine("The longest sequence of equal elements starts at position {0}"
  45.                 + " and it is {1} elements long",startPosition,maxLenght);
  46.  
  47.             for (int i = 0; i < maxLenght; i++)
  48.             {
  49.                 Console.Write(array[startPosition] + " ");
  50.             }
  51.  
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement