Advertisement
sashomaga

Pillars

Dec 23rd, 2012
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 KB | None | 0 0
  1. using System;
  2.  
  3. class Program
  4. {
  5. //You are given a list of 8 bytes (positive integers in the range [0…255]) n0, n1, …, n7. These numbers represent a square grid consisting of 8 lines and 8 columns.
  6. //Each cell of the grid could either be empty or full. The first line is represented by the bits of n0, the second – by the bits of n1 and so on, and the last
  7. //line is represented by the bits of n7. Each bit with value 1 denotes a full cell and each bit with value 0 denotes an empty cell. The lines are numbered
  8. //from the first (top) to the last (bottom) with the numbers 0, 1, …, 7. The columns are numbered from right to left with the indices 0, 1, …, 7.
  9. //The figure shows a square grid and its representation by a sequence of 8 numbers n0, n1, …, n7:
  10. //    7 6   5   4   3   2   1   0  
  11. //0                                 n0 = 0
  12. //1     ■                         n1 = 64
  13. //2                                 n2 = 0
  14. //3                 ■             n3 = 8
  15. //4                                 n4 = 0
  16. //5                 ■ ■         n5 = 12
  17. //6 ■ ■ ■                     n6 = 224
  18. //7                                 n7 = 0
  19. //We are allowed to put a vertical pillar over any of the columns in the grid. Pillars split the grid into two sides (left and right) and the column holding the
  20. //pillar is ignored. Write a program that finds the leftmost column where the pillar can be put so that the full cells on the left side and on the right side
  21. //are equal number. For example at the figure if we put the pillar at column 5, it will split the grid into two sides and both sides will have exactly 3 full cells.
  22. //Input
  23. //The input data should be read from the console.
  24. //There will be exactly 8 lines each holding the integer numbers n0, n1, …, n7.
  25. //The input data will always be valid and in the format described. There is no need to check it explicitly.
  26. //Output
  27. //The output data should be printed on the console.
  28. //If a pillar splitting the grid into two vertical sides each holding the same number of full cells exists, print its column index on the first line and the number of full
  29. //cells in each of the sides. If multiple pillars can do the job, print only the leftmost. If no such pillar exists, print the string "No" on the console
  30. //(just one line holding the word "No").
  31.  
  32.     static int[] n = new int[8];
  33.  
  34.  
  35.     static void Main()
  36.     {
  37.         int left = new int();
  38.         int right = new int();
  39.  
  40.         ReadData();
  41.        
  42.         for (int col = n.Length-1; col >= 0; col--) //start from 7th column
  43.         {
  44.            left = GetLeftSum(col);
  45.            right = GetRightSum(col);
  46.            if (left == right)
  47.            {
  48.                Console.WriteLine(col);
  49.                Console.WriteLine(left);
  50.                return;
  51.            }
  52.         }
  53.         Console.WriteLine("No");
  54.  
  55.     }
  56.  
  57.     private static int GetRightSum(int col)
  58.     {
  59.         int value = new int();
  60.         for (int i = col - 1; i >= 0; i--)
  61.         {
  62.             for (int j = 0; j < n.Length; j++) //loop data
  63.             {
  64.                 if ((n[j] >> i & 1) == 1)
  65.                 {
  66.                     value++;
  67.                 }
  68.             }
  69.         }
  70.         return value;
  71.     }
  72.  
  73.     private static int GetLeftSum(int col)
  74.     {
  75.         int value = new int();
  76.         if (col == 7) //case of 7th column
  77.         {
  78.             return 0;
  79.         }
  80.         for (int i = col + 1; i <= n.Length; i++)
  81.         {
  82.             for (int j = 0; j < n.Length; j++) //loop data
  83.             {
  84.                 if ((n[j] >> i & 1) == 1)
  85.                 {
  86.                     value++;
  87.                 }
  88.             }
  89.         }
  90.         return value;
  91.     }
  92.  
  93.     private static void ReadData()
  94.     {
  95.         for (int i = 0; i < n.Length; i++)
  96.         {
  97.             n[i] = int.Parse(Console.ReadLine());
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement