BorislavBorisov

38.01.Dancing Bits решение с битове

Oct 30th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. class DancingBits
  3. {
  4.     static void Main()
  5.     {
  6.         int k = int.Parse(Console.ReadLine());
  7.         int n = int.Parse(Console.ReadLine());
  8.         int len = 0, sequence = 0, lastBit = 0;
  9.         for (int i = 0; i < n; i++)
  10.         {
  11.             int nextNumber = int.Parse(Console.ReadLine());
  12.             bool isOne = false;
  13.             for (int g = 31; g >= 0; g--)
  14.             {
  15.                 int bit = nextNumber >> g & 1;
  16.                 if(bit == 1)
  17.                 {
  18.                     isOne = true;
  19.                 }
  20.                 if(isOne)
  21.                 {
  22.                     if(lastBit == bit)// ако започваме нова редица не влиза тук
  23.                     {
  24.                         len++;
  25.                     }
  26.                     else
  27.                     {
  28.                         if(len == k)
  29.                         {
  30.                             sequence++;
  31.                         }
  32.                         len = 1;//ако сме намерили поредица, започваме нова
  33.                     }
  34.                     lastBit = bit;
  35.                 }
  36.             }
  37.         }
  38.         if(len == k)//ако има поредица в края кажи!
  39.         {
  40.             sequence++;
  41.         }
  42.         Console.WriteLine(sequence);
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment