BorislavBorisov

04.01.Bit Flipper-мое решение

Oct 8th, 2015
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 KB | None | 0 0
  1. using System;
  2. class BitFlipper
  3. {
  4.     static void Main()
  5.     {
  6.         ulong N = ulong.Parse(Console.ReadLine());
  7.         string input = Convert.ToString((long)N, 2).PadLeft(64, '0');
  8.         string output = "";
  9.  
  10.         for (int i = 0; i < input.Length - 2; i++)
  11.         {
  12.             if (input[i] == input[i + 1] && input[i + 1] == input[i + 2])
  13.             {
  14.                 for (int j = 0; j < 3; j++)
  15.                 {
  16.                     if (input[i] == '1')
  17.                     {
  18.                         output += '0';
  19.                     }
  20.                     else
  21.                     {
  22.                         output += '1';
  23.                     }
  24.                 }
  25.                 //for (int j = i; j <= i + 2; j++)
  26.                 //{
  27.                 //    if (input[j] == '0')
  28.                 //    {
  29.                 //        output += '1';
  30.                 //    }
  31.                 //    else
  32.                 //    {
  33.                 //        output += '0';
  34.                 //    }
  35.                 //}
  36.                 i += 2;
  37.             }
  38.             else
  39.             {
  40.                 output += input[i];
  41.             }
  42.         }
  43.         int lengthPast = input.Length - output.Length;//добавям ако има останали в края
  44.         if (lengthPast != 0)
  45.         {
  46.             for (int i = lengthPast; i > 0; i--)
  47.             {
  48.                 output += input[input.Length - i];
  49.             }
  50.         }
  51.         //if (input.Length > output.Length)
  52.         //{
  53.         //    for (int i = output.Length; i < input.Length; i++)
  54.         //    {
  55.         //        output += input[i];
  56.         //    }
  57.         //}
  58.         Console.WriteLine(Convert.ToUInt64(output,2));
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment