Advertisement
Asinka

04. DancingBits (7 Dec 2011 Morning)

Dec 26th, 2012
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.23 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4.  
  5. class DancingBits
  6. {
  7.     static void Main()
  8.     {
  9.         int lenght = int.Parse(Console.ReadLine());
  10.         int digits = int.Parse(Console.ReadLine());
  11.         int previousBit = 0;
  12.         int count = 0;
  13.         int result = 0;
  14.  
  15.         for (int i = 0; i < digits; i++)
  16.         {
  17.             int number = int.Parse(Console.ReadLine());
  18.             for (int j = 30; j >= 0; j--)
  19.             {
  20.                 int mask = 1 << j;
  21.                 int bit = number & mask;
  22.                 bit = bit >> j;
  23.  
  24.                 if (mask > number)
  25.                 {
  26.                     continue;
  27.                 }
  28.                 else if (lenght == 1)
  29.                 {
  30.                     if (bit != previousBit)
  31.                     {
  32.                         count = 1;
  33.                         result++;
  34.                         previousBit = bit;
  35.                     }
  36.                     else if (bit == previousBit && count == 1)
  37.                     {
  38.                         count = 0;
  39.                         result--;
  40.                     }
  41.                 }
  42.                 else if (bit == previousBit && count == lenght)
  43.                 {
  44.                     if (j == lenght - 1 && i == digits - 1)
  45.                     {
  46.                         count++;
  47.                     }
  48.                     else
  49.                     {
  50.                         count = 1;
  51.                     }
  52.                 }
  53.                 else if (bit != previousBit && count == lenght)
  54.                 {
  55.                     count = 1;
  56.                     result++;
  57.                     previousBit = bit;
  58.                 }
  59.                 else if (bit == previousBit && count < lenght)
  60.                 {
  61.                     if (count == lenght - 1 && i == digits - 1 && j == 0)
  62.                     {
  63.                         result++;
  64.                     }
  65.                     else
  66.                     {
  67.                         count++;
  68.                     }
  69.                 }
  70.                 else if (bit != previousBit && count < lenght)
  71.                 {
  72.                     count = 1;
  73.                     previousBit = bit;
  74.                 }
  75.  
  76.             }
  77.         }
  78.         Console.WriteLine(result);
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement