ellapt

DancingBits

Dec 18th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. using System;
  2.  
  3. class DancingBits
  4. {
  5. static void Main()
  6. {
  7. int k = int.Parse(Console.ReadLine());
  8. int n = int.Parse(Console.ReadLine());
  9. int length = 0;
  10. int lastBit = -1;
  11. int bitsCount = 0;
  12. for (int i = 0; i < n; i++)
  13. {
  14. int number = int.Parse(Console.ReadLine());
  15.  
  16. int firstNonZeroBit = 0;
  17. for (int bitNumber = 31; bitNumber >= 0; bitNumber--)
  18. {
  19. int currentBit = (number >> bitNumber) & 1;
  20. if (currentBit == 1)
  21. {
  22. firstNonZeroBit = bitNumber;
  23. break;
  24. }
  25. }
  26.  
  27. for (int bitNumber = firstNonZeroBit; bitNumber >= 0; bitNumber--)
  28. {
  29. int currentBit = (number >> bitNumber) & 1;
  30. if (currentBit == lastBit)
  31. {
  32. length++;
  33. }
  34. else
  35. {
  36. if (length == k)
  37. {
  38. bitsCount++;
  39. }
  40. length = 1;
  41. }
  42. lastBit = currentBit;
  43. }
  44. }
  45.  
  46. if (length == k)
  47. {
  48. bitsCount++;
  49. }
  50.  
  51. Console.WriteLine(bitsCount);
  52.  
  53. }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment