kalinkata

Untitled

Oct 13th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class MaxSequenceOfEqualElements
  6. {
  7.     static void Main()
  8.     {
  9.         decimal[] nums = Console.ReadLine().Split().Select(decimal.Parse).ToArray();
  10.  
  11.         List<List<decimal>> sequences = new List<List<decimal>>();
  12.         List<decimal> sequence = new List<decimal>();
  13.  
  14.         int start = 0;
  15.         int end = 0;
  16.  
  17.         for (int i = 1; i < nums.Length; i++)
  18.         {
  19.             if (nums[i - 1] == nums[i])
  20.             {
  21.                 end++;
  22.             }
  23.             else if (end > 0)
  24.             {
  25.                 start = i - end - 1;
  26.                 end = i - 1;
  27.  
  28.                 AddSequence(nums, ref start, ref end, sequences, sequence);
  29.  
  30.                 sequence = new List<decimal>();
  31.             }
  32.  
  33.             if (i == nums.Length - 1 && end > 0)
  34.             {
  35.                 start = i - end;
  36.                 end = i;
  37.  
  38.                 AddSequence(nums, ref start, ref end, sequences, sequence);
  39.  
  40.                 sequence = new List<decimal>();
  41.             }
  42.         }
  43.  
  44.         decimal max = decimal.MinValue;
  45.  
  46.         for (int i = 0; i < sequences.Count; i++)
  47.         {
  48.             if (max < sequences[i].Count)
  49.             {
  50.                 max = sequences[i].Count;
  51.             }
  52.         }
  53.  
  54.         foreach (var item in sequences)
  55.         {
  56.             if (item.Count == max)
  57.             {
  58.                 Console.WriteLine(String.Join(" ", item));
  59.  
  60.                 break;
  61.             }
  62.         }
  63.     }
  64.  
  65.     private static void AddSequence(decimal[] nums, ref int start, ref int end, List<List<decimal>>sequences, List<decimal> sequence)
  66.     {
  67.         for (int a = start; a <= end; a++)
  68.         {
  69.             sequence.Add(nums[a]);
  70.         }
  71.  
  72.         sequences.Add(sequence);
  73.  
  74.         start = 0;
  75.         end = 0;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment