Advertisement
vic_alexiev

FallDown.cs

Dec 18th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.18 KB | None | 0 0
  1. using System;
  2.  
  3. class FallDown
  4. {
  5.     static void Main()
  6.     {
  7.         int n = 8;
  8.  
  9.         uint[] numbers = new uint[n];
  10.  
  11.         for (int i = 0; i < n; i++)
  12.         {
  13.             string number = Console.ReadLine();
  14.             numbers[i] = UInt32.Parse(number);
  15.         }
  16.  
  17.         int[] onesInCol = new int[n];
  18.  
  19.         // get the 1's count in each column
  20.         for (int col = 0; col < n; col++)
  21.         {
  22.             uint mask = 1U << col;
  23.  
  24.             for (int i = 0; i < n; i++)
  25.             {
  26.                 if ((numbers[i] & mask) != 0)
  27.                 {
  28.                     onesInCol[col]++;
  29.                 }
  30.             }
  31.         }
  32.  
  33.         uint[] newNumbers = new uint[n];
  34.  
  35.         // set the 1's in the respective columns of the numbers - bottom-up
  36.         for (int col = 0; col < n; col++)
  37.         {
  38.             int i = n - 1;
  39.             uint mask = 1U << col;
  40.             while (onesInCol[col] > 0)
  41.             {
  42.                 newNumbers[i] |= mask;
  43.                 onesInCol[col]--;
  44.                 i--;
  45.             }
  46.         }
  47.  
  48.         for (int i = 0; i < n; i++)
  49.         {
  50.             Console.WriteLine(newNumbers[i]);
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement