Advertisement
jokerbg

Fall Down solution

Dec 5th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using System;
  2. class FallDown
  3. {
  4.     static void Main()
  5.     {
  6.         int[] initial = new int[8];
  7.         int[] afterFall = new int[8];
  8.         int[] countFull = new int[8];
  9.  
  10.         //enter initial field
  11.         for (int i = 0; i < initial.Length; i++)
  12.         {
  13.             initial[i] = int.Parse(Console.ReadLine());
  14.         }
  15.  
  16.         //count full cells on each position
  17.         for (int shift = 0; shift < 8; shift++)
  18.         {
  19.             for (int element = 0; element < 8; element++)
  20.             {
  21.                 if (((initial[element] >> shift) & 1) == 1)
  22.                 {
  23.                     countFull[shift]++;
  24.                 }
  25.             }
  26.         }
  27.  
  28.         //fill positions
  29.         for (int shift = 0; shift < 8; shift++)
  30.         {
  31.             for (int i = 7; i >= 0; i--)
  32.             {
  33.                 if (countFull[shift] > 0)
  34.                 {
  35.                     afterFall[i] = afterFall[i] | (1 << shift);
  36.                     countFull[shift]--;
  37.                 }
  38.                 else
  39.                 {
  40.                     break;
  41.                 }
  42.             }
  43.         }
  44.  
  45.         //write output
  46.         foreach (int ready in afterFall)
  47.         {
  48.             Console.WriteLine(ready);
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement