Advertisement
sylviapsh

Fall Down

Dec 27th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using System;
  2. class FallDown
  3. {
  4.   static void Main()
  5.   {
  6.     //Telerik Academy
  7.     //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. 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 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 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. The figure shows a sample square grid and its representation by a sequence of 8 numbers n0, n1, …, n7:
  8.     //  7   6   5   4   3   2   1   0                
  9. //0                                 n0 = 0 
  10. //1     ■                         n1 = 64
  11. //2                 ■             n2 = 8 
  12. //3                 ■             n3 = 8 
  13. //4                                 n4 = 0 
  14. //5                 ■ ■         n5 = 12
  15. //6 ■ ■ ■                     n6 = 224
  16. //7                                 n7 = 0 
  17.  
  18. //  7   6   5   4   3   2   1   0  
  19. //0                                 n0 = 0
  20. //1                                 n1 = 0
  21. //2                                 n2 = 0
  22. //3                                 n3 = 0
  23. //4                                 n4 = 0
  24. //5                 ■             n5 = 8
  25. //6     ■         ■             n6 = 72
  26. //7 ■ ■ ■     ■ ■         n7 = 236
  27.  
  28.     //Suppose the full cells hold squares which can "fall down" by the influence of the gravity. Each full cell in certain row and column falls down to the lowest row possible but stays in the same column and up from any other full cells on the same column that ware initially down from it. At the figure the "fall down" process is illustrated.
  29.     //Write a program to calculate how the grid will look like after the "fall down" process is applied.
  30.  
  31.     int inputNum = 0,
  32.         bitsSum = 0,
  33.         bufferNum = 0;
  34.     int[] numsArray = new int[8];
  35.     int[] fallDownNumsArray = new int[8];
  36.  
  37.     //Read the number from the console and input them in an array
  38.     for (int inputIndex = 0; inputIndex <= 7; inputIndex++)
  39.     {
  40.       inputNum = int.Parse(Console.ReadLine());
  41.       numsArray[inputIndex] = inputNum;
  42.     }
  43.  
  44.     //Check how many 1 there are in each column and create the column numbers.
  45.     for (int bitIndex = 0; bitIndex <= 7; bitIndex++)
  46.     {
  47.       for (int arrayElemIndex = 0; arrayElemIndex <= 7; arrayElemIndex++)
  48.       {
  49.         bitsSum += ((numsArray[arrayElemIndex] & (1 << bitIndex)) >> bitIndex);
  50.       }
  51.       for (int fallNumIndex = 0; fallNumIndex < bitsSum; fallNumIndex++)
  52.       {
  53.         bufferNum = (bufferNum | (1 << fallNumIndex));
  54.       }
  55.       fallDownNumsArray[bitIndex] = bufferNum;
  56.       bufferNum = 0;
  57.       bitsSum = 0;
  58.     }
  59.  
  60.     for (int bitIndex = 0; bitIndex <= 7; bitIndex++)
  61.     {
  62.       bufferNum = 0;
  63.       for (int arrayIndex = 0; arrayIndex <= 7; arrayIndex++)
  64.       {
  65.         int bitAtPos = ((fallDownNumsArray[arrayIndex] & (1 << bitIndex)) >> bitIndex);
  66.         if (bitAtPos == 1)
  67.         {
  68.           bufferNum = (bufferNum | (1 << arrayIndex));
  69.         }
  70.       }
  71.       numsArray[bitIndex] = bufferNum;
  72.     }
  73.  
  74.     for (int i = 7; i >= 0; i--)
  75.     {
  76.       Console.WriteLine(numsArray[i]);
  77.     }
  78.   }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement