fbinnzhivko

SaltAndPepper 2

Apr 22nd, 2016
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.79 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. class SaltAndPepper
  4. {
  5.     static void Main()
  6.     {
  7.         ulong dishes = ulong.Parse(Console.ReadLine());
  8.         string dishesToBits = ConvertToBinary(dishes).PadLeft(64, '0');
  9.         char[] bits = dishesToBits.ToCharArray();
  10.  
  11.         string line = Console.ReadLine();
  12.  
  13.         do
  14.         {
  15.             string[] getLine = line.Split();
  16.             string command = getLine[0];
  17.             if (command == "end")
  18.             {
  19.                 break;
  20.             }
  21.             int step = int.Parse(getLine[1]);
  22.  
  23.             if (step > 0)
  24.             {
  25.                 for (int i = bits.Length - 1; i >= 0; i -= step)
  26.                 {
  27.                     if (command == "salt" && bits[i] == '1')
  28.                     {
  29.                         bits[i] = '0';
  30.                     }
  31.                     else if (command == "pepper" && bits[i] == '0')
  32.                     {
  33.                         bits[i] = '1';
  34.                     }
  35.                 }
  36.             }
  37.             else
  38.             {
  39.                 line = Console.ReadLine();
  40.                 continue;
  41.             }
  42.  
  43.             line = Console.ReadLine();
  44.         } while (line != "end");
  45.  
  46.         StringBuilder addBits = new StringBuilder();
  47.  
  48.         for (int i = 0; i < bits.Length; i++)
  49.         {
  50.             addBits.Append(bits[i]);
  51.         }
  52.         ulong res = Convert.ToUInt64(addBits.ToString(), 2);
  53.         Console.WriteLine(res);
  54.     }
  55.  
  56.     static string ConvertToBinary(ulong value)
  57.     {
  58.         if (value == 0) return "0";
  59.         System.Text.StringBuilder b = new System.Text.StringBuilder();
  60.         while (value != 0)
  61.         {
  62.             b.Insert(0, ((value & 1) == 1) ? '1' : '0');
  63.             value >>= 1;
  64.         }
  65.         return b.ToString();
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment