Advertisement
archangelmihail

FallDownWithoutBits

Dec 1st, 2013
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.72 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 FallDownWithoutBits
  8. {
  9.     class FallDownWithoutBits
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             // solution with bit operations
  14.             //input
  15.             int[,] matrix = new int[8,8];
  16.            
  17.             for (int row = 0; row < 8; row++)
  18.             {
  19.                 int number = int.Parse(Console.ReadLine());
  20.                 string numberToString = Convert.ToString(number, 2).PadLeft(8, '0');
  21.                 for (int col = 0; col < 8; col++)
  22.                 {
  23.                     matrix[row, col] = int.Parse(numberToString[col].ToString());
  24.                 }
  25.             }
  26.            
  27.  
  28.             //body
  29.             for (int col = 0; col < 8; col++)
  30.             {
  31.                 int count = 0;
  32.                 for (int row = 0; row < 8 ; row++)
  33.                 {
  34.                     if (matrix [row,col] == 1)
  35.                     {
  36.                         count++;
  37.                         matrix[row, col] = 0;
  38.                     }
  39.  
  40.                 }
  41.                 for (int row = 0; row < count; row++)
  42.                 {
  43.                         matrix[7-row, col] = 1;
  44.                 }
  45.             }
  46.             //output
  47.             for (int row = 0; row < 8; row++)
  48.             {
  49.                 StringBuilder sb = new StringBuilder();
  50.                 for (int col = 0; col < 8; col++)
  51.                 {
  52.                     sb.Append(matrix[row,col]);
  53.                 }
  54.                 int number = Convert.ToInt32(sb.ToString(),2);
  55.                 Console.WriteLine(number);
  56.             }
  57.            
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement