BorislavBorisov

26.Wave Bits-мое решение

Oct 13th, 2015
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2. class WaveBits
  3. {
  4.     static void Main()
  5.     {
  6.         ulong N = ulong.Parse(Console.ReadLine());
  7.        
  8.         bool isFindSeq = false, isStartIndex = true;
  9.         int counter = 1, bestCounter = 0, startDelete = 0, otherStartDelete = 0;
  10.         for (int i = 64; i >= 0; i--)
  11.         {
  12.             if((N & ((ulong)1 << i)) != 0 && (N & ((ulong)1 << i - 1)) == 0
  13.                 && (N & ((ulong)1 << i - 2)) != 0)
  14.             {                
  15.                 counter += 2;                
  16.                 if(isStartIndex)
  17.                 {
  18.                     isStartIndex = false;                    
  19.                     startDelete = i;
  20.                 }
  21.                 i--;
  22.             }
  23.             if(i == 0 && bestCounter > counter)
  24.             {
  25.                 startDelete = otherStartDelete;
  26.             }
  27.             else if((N & ((ulong)1 << i)) != 0 && !(isFindSeq) && counter > 1)
  28.             {
  29.                 if (bestCounter <= counter)
  30.                 {  
  31.                     bestCounter = counter;
  32.                     isStartIndex = true;
  33.                     otherStartDelete = startDelete;
  34.                 }
  35.                 counter = 1;                
  36.             }
  37.         }
  38.         if(bestCounter == 0)
  39.         {
  40.             Console.WriteLine("No waves found!");
  41.             return;
  42.         }
  43.         string newNumber = null;
  44.         for (int i = 63; i >= 0; i--)
  45.         {
  46.             if(i  == startDelete)
  47.             {
  48.                 i++;
  49.                 while(bestCounter > 0)//пор този начин не броя намерената редица
  50.                 {
  51.                     bestCounter--;
  52.                     i--;
  53.                 }
  54.             }
  55.             else if((N & ((ulong)1 << i)) != 0)
  56.             {
  57.                 newNumber += '1';
  58.             }
  59.             else if((N & ((ulong)1 << i)) == 0)
  60.             {
  61.                 newNumber += '0';
  62.             }
  63.         }      
  64.         Console.WriteLine(Convert.ToUInt64(newNumber, 2));
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment