ralitsa_d

[Programming Basics Exam - 17 January 2016] Problem 05 - Eme

Apr 11th, 2016
127
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. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class EmergencyRepairs
  8. {
  9.     static void Main()
  10.     {
  11.         ulong wall = ulong.Parse(Console.ReadLine());
  12.         int emergencyKits = int.Parse(Console.ReadLine());
  13.         int numberOfAttacks = int.Parse(Console.ReadLine());
  14.  
  15.         //determine damages after the attacks
  16.         for (int i = 0; i < numberOfAttacks; i++)
  17.         {
  18.             int attack = int.Parse(Console.ReadLine());
  19.             if (((wall >> attack) & 1) == 1)
  20.             {
  21.                 ulong mask = ~((ulong)1 << attack);
  22.                 wall = wall & mask;
  23.             }
  24.         }
  25.  
  26.         //find big holes
  27.         int bitCounter = 0;
  28.         while (emergencyKits > 0)
  29.         {
  30.             if (((wall >> bitCounter) & 1) == 0 &&
  31.                 (((wall >> (bitCounter + 1)) & 1) == 0))
  32.             {
  33.                 ulong mask = (ulong)1 << bitCounter;
  34.                 wall = wall | mask;
  35.                 emergencyKits--;
  36.  
  37.                 if (emergencyKits > 0)
  38.                 {
  39.                     bitCounter++;
  40.                     mask = (ulong)1 << bitCounter;
  41.                     wall = wall | mask;
  42.                     emergencyKits--;
  43.                 }
  44.             }
  45.             bitCounter++;
  46.         }        
  47.  
  48.         //find small holes
  49.         bitCounter = 0;
  50.         while (emergencyKits > 0)
  51.         {
  52.             if (((wall >> bitCounter) & 1) == 0)
  53.             {
  54.                 ulong mask = (ulong)1 << bitCounter;
  55.                 wall = wall | mask;
  56.                 emergencyKits--;
  57.             }
  58.             bitCounter++;
  59.         }
  60.        
  61.         Console.WriteLine(wall);
  62.  
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment