Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 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 ConsoleJustification
  8. {
  9.  
  10.     class ConsoleJustification
  11.     {
  12.         private static char[] separators = new char[] { ' ' };
  13.         static int lineWidth;
  14.  
  15.         static void Main()
  16.         {
  17.             int linesCount = int.Parse(Console.ReadLine());
  18.             lineWidth = int.Parse(Console.ReadLine());
  19.             string[] lines = new string[linesCount];
  20.             for (int i = 0; i < linesCount; i++)
  21.             {
  22.                 lines[i] = Console.ReadLine();
  23.             }
  24.  
  25.             string[] array = GetWords(lines);
  26.             MadeRows(array);
  27.         }
  28.  
  29.         private static void MadeRows(string[] words)
  30.         {
  31.             StringBuilder sb = new StringBuilder();
  32.             Queue<string> queue = new Queue<string>();
  33.             for (int i = 0; i < words.Length; i++)
  34.             {
  35.                 queue.Enqueue(words[i]);
  36.             }
  37.             int currentRowLength = 0;
  38.             int wordsCount = 0;
  39.             string currentWord = String.Empty;
  40.             List<string> current = new List<string>();
  41.             while (true)
  42.             {
  43.                 if (!queue.Any())
  44.                 {
  45.                     Console.WriteLine(currentWord.Trim());
  46.                     break;
  47.                 }
  48.                 currentWord = queue.Peek();
  49.                 currentRowLength += currentWord.Length + 1;
  50.                 wordsCount++;
  51.                 if (currentRowLength <= lineWidth + 1)
  52.                 {
  53.                     current.Add(queue.Dequeue());
  54.                 }
  55.                 else
  56.                 {
  57.                     int spaces = lineWidth - String.Join("", current).Trim().Length;
  58.                     int i = 0;
  59.                     while (spaces > 0)
  60.                     {
  61.                         if (current.Count > 1)
  62.                         {
  63.                             if (i == current.Count - 1)
  64.                             {
  65.                                 i = 0;
  66.                             }
  67.                             if (i < current.Count - 1)
  68.                             {
  69.                                 current[i] = current[i] + " ";
  70.                                 spaces--;
  71.                             }
  72.                             i++;
  73.                         }
  74.                         else
  75.                         {
  76.                             break;
  77.                         }
  78.                     }
  79.                     Console.WriteLine(String.Join("",current).Trim());
  80.                     current.Clear();
  81.                     currentRowLength = 0;
  82.                 }
  83.  
  84.             }
  85.         }
  86.         private static string[] GetWords(string[] lines)
  87.         {
  88.             List<string> words = new List<string>();
  89.  
  90.             for (int i = 0; i < lines.Length; i++)
  91.             {
  92.                 string[] currentWords = lines[i].Split(separators, StringSplitOptions.RemoveEmptyEntries);
  93.                 for (int j = 0; j < currentWords.Length; j++)
  94.                 {
  95.                     words.Add(currentWords[j]);
  96.                 }
  97.             }
  98.             return words.ToArray();
  99.         }
  100.  
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement