psotirov

Pillars

Dec 9th, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.03 KB | None | 0 0
  1. using System;
  2.  
  3. class Pillars
  4. {
  5.     static void Main()
  6.     {
  7.         int[] grid = new int[8] {0, 0, 0, 0, 0, 0, 0, 0};
  8.         for (int i = 0; i < 8; i++) // Loop to enter byte masks
  9.         {
  10.             int number = int.Parse(Console.ReadLine()); // reads a number from the console
  11.             for (int j = 0; j < 8; j++) // interates through the number's bits
  12.             {
  13.                 if (((number >> j) & 1) == 1) // if the bit in j-th position is set  
  14.                 {
  15.                     grid[j]++; // increases the array counter in position j
  16.                 }
  17.             }
  18.         } // finally in each element of the array "grid" we will have the count of bits into correspondng "pillar"
  19.  
  20.         int pillarPos = -1;
  21.         int leftSum = 0; // the sum of bits for pillars that are located left to the result
  22.  
  23.         for (int i = 7; i >= 0; i--) // Main loop - interates through the pillars (from most to least positon)
  24.         {
  25.             leftSum = 0; // the sum of bits for pillars that are located left to the current one
  26.             int rightSum = 0; // the sum of bits for pillars that are located right to the current one
  27.             for (int j = 7; j > i; j--) // Left pillars loop / sum, if i=7 there are no left columns, leftSum=0
  28.             {
  29.                 leftSum += grid[j];
  30.             }                
  31.  
  32.             for (int j = 0; j < i; j++) // Right pillars loop / sum, if i=0 there are no right columns, rightSum=0
  33.             {
  34.                 rightSum += grid[j];
  35.             }                
  36.  
  37.  
  38.             if (leftSum == rightSum) // if we have a solution
  39.             {
  40.                 pillarPos = i; // i-th index is the position of the "winner"pillar, the number of bits is kept ito "leftSum" variable
  41.                 break; // and stops the loop
  42.             }
  43.         }
  44.         if (pillarPos == -1)
  45.         {
  46.             Console.WriteLine("No");
  47.         }
  48.         else
  49.         {
  50.             Console.WriteLine(pillarPos);
  51.             Console.WriteLine(leftSum);
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment