Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Напишете програма която намира и отпечатва максималната по дължина поредица от еднакви елементи в масив.
- Пример: { 2, 1, 1, 2, 3, 3, 2, 2, 2, 1 } -> { 2, 2, 2 } */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _05_MaxSamelNumbersInRange
- {
- class MaxSamelNumbersInRange
- {
- static void Main()
- {
- Console.Write("N = ");
- int n = Int32.Parse(Console.ReadLine());
- int[] array = new int[n];
- for (int i = 0; i < n; i++)
- {
- Console.Write("Element {0} = ", i+1);
- array[i] = Int32.Parse(Console.ReadLine());
- }
- int maxLenght = 0;
- int startPosition = 0;
- for (int i = 0; i < array.Length; i++)
- {
- int j = i + 1;
- while (j < array.Length && array[i] == array[j])
- {
- j++;
- }
- int length = j - i;
- if (length > maxLenght)
- {
- maxLenght = length;
- startPosition = i;
- }
- }
- Console.WriteLine("The longest sequence of equal elements starts at position {0}"
- + " and it is {1} elements long",startPosition,maxLenght);
- for (int i = 0; i < maxLenght; i++)
- {
- Console.Write(array[startPosition] + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement