BorislavBorisov

38.03.Dancing Bits решение със стринг

Oct 30th, 2015
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System;
  2. class DancingBits
  3. {
  4.     static void Main()
  5.     {
  6.         int k = 3;
  7.         int n = 4;
  8.         string allBits = "";
  9.         for (int i = 0; i < n; i++)
  10.         {
  11.             int input = int.Parse(Console.ReadLine());
  12.             allBits += Convert.ToString(input, 2);
  13.         }
  14.         allBits = "*" + allBits + "*";
  15.         string ones = new string('1', k);
  16.  
  17.         int oneCount = 0;
  18.         int index = -1;
  19.         while(true)
  20.         {
  21.             index = allBits.IndexOf(ones, index + 1);
  22.             if(index == -1)
  23.             {
  24.                 break;
  25.             }
  26.             if(allBits[index + k - 1] != allBits[index + k]
  27.                 && allBits[index - 1] != allBits[index])
  28.             {
  29.                 oneCount++;
  30.             }
  31.         }
  32.         string zeros = new string('0', k);
  33.         int zerosCount = 0;
  34.        
  35.         while(true)
  36.         {
  37.             index = allBits.IndexOf(zeros, index + 1);
  38.             if(index == -1)
  39.             {
  40.                 break;
  41.             }
  42.             if (allBits[index + k - 1] != allBits[index + k]
  43.                 && allBits[index - 1] != allBits[index])
  44.             {
  45.                 zerosCount++;
  46.             }
  47.         }
  48.         Console.WriteLine(zerosCount + oneCount);
  49.     }
  50. }
  51. /*
  52. 3
  53. 4
  54. 5
  55. 6
  56. 14
  57. 143
  58.  */
Advertisement
Add Comment
Please, Sign In to add comment