Advertisement
DonJabone

MagicWords

Dec 26th, 2013
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4. using System.Collections.Generic;
  5.  
  6. class MagicWords
  7. {
  8.     static void Main()
  9.     {
  10.         int numberOfWords = int.Parse(Console.ReadLine());
  11.         string[] words = new string[numberOfWords];
  12.  
  13.         for (int i = 0; i < numberOfWords; i++)
  14.         {
  15.             words[i] = Console.ReadLine();
  16.         }
  17.  
  18.         words = Reorder(words, numberOfWords);
  19.         Console.WriteLine(Print(words));
  20.     }
  21.  
  22.     private static string[] Reorder(string[] words, int numberOfWords)
  23.     {
  24.         List<string> list = words.OfType<string>().ToList();
  25.  
  26.         for (int i = 0; i < list.Count; i++)
  27.         {
  28.             string currentWord = list[i];
  29.             int newIndex = (currentWord.Length % (numberOfWords + 1));
  30.  
  31.             if (newIndex == -1)
  32.             {
  33.                 newIndex = 0;
  34.             }
  35.  
  36.             string buffer = list[i];
  37.             list[i] = null;
  38.             list.Insert(newIndex, buffer);
  39.             list.Remove(null);
  40.         }
  41.  
  42.         words = list.ToArray();
  43.  
  44.         return words;
  45.     }
  46.  
  47.     private static string Print(string[] words)
  48.     {
  49.         StringBuilder result = new StringBuilder();
  50.         int index = 0;
  51.  
  52.         var sorted = words.OrderBy(n => n.Length);
  53.         var longest = sorted.LastOrDefault();
  54.  
  55.         while (index < longest.Length)
  56.         {
  57.             for (int i = 0; i < words.Length; i++)
  58.             {
  59.                 string currentWord = words[i];
  60.  
  61.                 if (index < currentWord.Length)
  62.                 {
  63.                     result.Append(currentWord[index]);
  64.                 }
  65.             }
  66.  
  67.             index++;
  68.         }
  69.  
  70.         return result.ToString();
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement