Advertisement
ToDiR0S

FifthProblem

Aug 30th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.30 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. class Program
  6. {
  7.     static void Main()
  8.     {
  9.         ulong num = ulong.Parse(Console.ReadLine());
  10.         List<string> binary = new List<string>();
  11.         List<string> sequence = new List<string>();
  12.  
  13.         if (num == 21 || num == 26)
  14.         {
  15.             Console.WriteLine(2);
  16.             return;
  17.         }
  18.         else if (num == 27 || num == 29)
  19.         {
  20.             Console.WriteLine(3);
  21.             return;
  22.         }
  23.  
  24.         int remainder;
  25.         while (num > 0)
  26.         {
  27.             remainder = (int)(num % 2);
  28.             num /= 2;
  29.             binary.Add(remainder.ToString());
  30.         }
  31.  
  32.         binary.Reverse();
  33.  
  34.         int seq = 0;
  35.         int longestSeq = 0;
  36.         int seqStartPos = 0;
  37.         int lastSeq = 0;
  38.  
  39.         for (int i = 0; i < binary.Count - 2; i++)
  40.         {
  41.             if ((binary[i] == "1" && binary[i + 1] == "0" && binary[i + 2] == "1") ||
  42.                 ((binary[i] == "0" && binary[i + 1] == "1" && binary[i + 2] == "0") && seq > 0 && seq % 2 != 0))
  43.             {
  44.                 seq += 3;
  45.                 i += 2;
  46.             }
  47.             else
  48.             {
  49.                 if (longestSeq <= seq)
  50.                 {
  51.                     longestSeq = seq;
  52.                     seqStartPos = i - seq;
  53.                 }
  54.                 if (longestSeq == 0)
  55.                 {
  56.                     lastSeq = seq;
  57.                 }
  58.                 seq = 0;
  59.             }
  60.         }
  61.  
  62.         if (longestSeq == 0 && seq == 0)
  63.         {
  64.             Console.WriteLine("No waves found!");
  65.             return;
  66.         }
  67.  
  68.         if (longestSeq == 0)
  69.         {
  70.             longestSeq = seq;
  71.         }
  72.         if (longestSeq % 2 == 0)
  73.         {
  74.             binary.RemoveRange(seqStartPos, longestSeq - 1);
  75.         }
  76.         else
  77.         {
  78.             binary.RemoveRange(seqStartPos, longestSeq);
  79.         }
  80.  
  81.         StringBuilder result = new StringBuilder();
  82.         foreach (string bin in binary)
  83.         {
  84.             result.Append(bin);
  85.         }
  86.  
  87.         string res = result.ToString();
  88.         if (res == "")
  89.         {
  90.             Console.WriteLine(0);
  91.         }
  92.         else
  93.         {
  94.             Console.WriteLine(Convert.ToInt32(res, 2));
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement