Advertisement
Filkolev

Text Gravity

May 27th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Security;
  4.  
  5. public class TextGravity
  6. {
  7.     public static void Main()
  8.     {
  9.         int lineLength = int.Parse(Console.ReadLine());
  10.         string text = Console.ReadLine();
  11.  
  12.         int index = 0;
  13.  
  14.         List<char[]> matrix = new List<char[]>();
  15.  
  16.         while (index < text.Length)
  17.         {
  18.             char[] currentLine = new char[lineLength];
  19.  
  20.             for (int i = 0; i < lineLength; i++)
  21.             {
  22.                 if (index < text.Length)
  23.                 {
  24.                     currentLine[i] = text[index];
  25.                 }
  26.                 else
  27.                 {
  28.                     currentLine[i] = ' ';
  29.                 }
  30.                
  31.                 index++;
  32.             }
  33.  
  34.             matrix.Add(currentLine);
  35.         }
  36.  
  37.         for (int row = matrix.Count - 1; row >= 0; row--)
  38.         {
  39.             for (int column = 0; column < lineLength; column++)
  40.             {
  41.                 if (matrix[row][column] != ' ')
  42.                 {
  43.                     continue;
  44.                 }
  45.  
  46.                 int currentRow = row - 1;
  47.                 while (currentRow >= 0)
  48.                 {
  49.                     if (matrix[currentRow][column] != ' ')
  50.                     {
  51.                         matrix[row][column] = matrix[currentRow][column];
  52.                         matrix[currentRow][column] = ' ';
  53.                         break;
  54.                     }
  55.  
  56.                     currentRow--;
  57.                 }
  58.             }
  59.         }
  60.  
  61.         Console.Write("<table>");
  62.  
  63.         for (int row = 0; row < matrix.Count; row++)
  64.         {
  65.             Console.Write("<tr>");
  66.             for (int column = 0; column < lineLength; column++)
  67.             {
  68.                 Console.Write("<td>{0}</td>", SecurityElement.Escape(matrix[row][column].ToString()));
  69.             }
  70.  
  71.             Console.Write("</tr>");
  72.         }
  73.  
  74.         Console.WriteLine("</table>");
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement