Advertisement
Guest User

30.08 Exam - Problem 5

a guest
Sep 1st, 2015
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace waveBitsv2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             ulong number = ulong.Parse(Console.ReadLine());
  14.             int count = 0;
  15.             int longestCount = 0;
  16.             int bestPosition = 0;
  17.             int currentPosition = 0;
  18.             bool seqStarted = false;
  19.  
  20.             int lastDigit = -1;
  21.             int currentDigit;
  22.  
  23.             for (int i = 0; i < 64; i++)
  24.             {
  25.                 currentDigit = CheckBit(number, i);
  26.  
  27.                 if ((currentDigit == 1 && currentDigit != lastDigit) && (seqStarted == false))
  28.                 {
  29.                     count++;
  30.                     seqStarted = true;
  31.                     currentPosition = i;
  32.                 }
  33.  
  34.                 else if (seqStarted == true && currentDigit != lastDigit)
  35.                 {
  36.                     count++;
  37.                 }
  38.  
  39.                 else
  40.                 {
  41.                     if (longestCount < count)
  42.                     {
  43.                         longestCount = count;
  44.                         bestPosition = currentPosition;
  45.                     }
  46.                     count = 0;
  47.                     seqStarted = false;
  48.  
  49.                 }
  50.  
  51.                 lastDigit = currentDigit;
  52.             }
  53.  
  54.  
  55.            
  56.  
  57.             if (longestCount == 1)
  58.             {
  59.                 Console.WriteLine("No waves found!");
  60.             }
  61.  
  62.             else
  63.             {
  64.                 number = RemoveBitV2(number, bestPosition, longestCount);
  65.                 Console.WriteLine(number);
  66.             }
  67.  
  68.            
  69.             //Console.WriteLine(longestCount);
  70.             //Console.WriteLine(bestPosition);
  71.         }
  72.  
  73.         public static int CheckBit(ulong number, int position)
  74.         {
  75.             if ((number & (ulong)1 << position) == 0)
  76.             {
  77.                 return 0;
  78.             }
  79.             else
  80.             {
  81.                 return 1;
  82.             }
  83.         }
  84.  
  85.         public static ulong RemoveBitV2(ulong number, int position, int count)
  86.         {
  87.             ulong mask = 0;
  88.  
  89.             for (int i = 0; i < position; i++)
  90.             {
  91.                 mask <<= 1;
  92.                 mask |= 1;
  93.             }
  94.  
  95.             ulong rightBits = number & mask;
  96.  
  97.             number >>= position + count;
  98.             number <<= position;
  99.             return number |= rightBits;
  100.  
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement