archangelmihail

FallDownWithSorting

Dec 1st, 2013
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.82 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FallDownWithSorting
  8. {
  9.     class FallDownWithSorting
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // solution with bit operations
  14.             //input
  15.             int[,] matrix = new int[8, 8];
  16.             for (int row = 0; row < 8; row++)
  17.             {
  18.                 int number = int.Parse(Console.ReadLine());
  19.                 string numberToString = Convert.ToString(number, 2).PadLeft(8, '0');
  20.                 for (int col = 0; col < 8; col++)
  21.                 {
  22.                     matrix[row, col] = int.Parse(numberToString[col].ToString());
  23.                 }
  24.             }
  25.             //solution
  26.             int[] tempMatrix = new int [8];
  27.             for (int col = 0; col < 8; col++)
  28.             {
  29.                
  30.                 for (int row = 0; row < 8; row++)
  31.                 {
  32.                     if (matrix[row, col] == 1)
  33.                     {
  34.                         tempMatrix[row] = 1;
  35.                     }
  36.                 }
  37.                 Array.Sort(tempMatrix);  // in beggining the lowest numbers, in the end the biggest
  38.                 for (int row = 0; row < 8; row++)
  39.                 {
  40.                     matrix[7- row, col] = tempMatrix[7-row];
  41.                     tempMatrix[7-row] = 0;
  42.                 }
  43.             }
  44.             //output
  45.             for (int row = 0; row < 8; row++)
  46.             {
  47.                 StringBuilder sb = new StringBuilder();
  48.                 for (int col = 0; col < 8; col++)
  49.                 {
  50.                     sb.Append(matrix[row, col]);
  51.                 }
  52.                 int number = Convert.ToInt32(sb.ToString(), 2);
  53.                 Console.WriteLine(number);
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment