Advertisement
Guest User

Magic Strings Alternative

a guest
Jun 3rd, 2015
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. public class MagicStrings
  5. {
  6.     static void Main()
  7.     {
  8.         int diff = int.Parse(Console.ReadLine());
  9.  
  10.         Dictionary<char, int> values = new Dictionary<char, int>();
  11.         values.Add('k', 1);
  12.         values.Add('n', 4);
  13.         values.Add('p', 5);
  14.         values.Add('s', 3);
  15.  
  16.         string firstSequence = string.Empty;
  17.         string secondSequence = string.Empty;
  18.         int firstSum = 0;
  19.         int secondSum = 0;
  20.         bool found = false;
  21.  
  22.         if (diff > 16)
  23.         {
  24.             Console.WriteLine("No");
  25.             return;
  26.         }
  27.  
  28.         foreach (KeyValuePair<char, int> first in values)
  29.         {
  30.             foreach (KeyValuePair<char, int> second in values)
  31.             {
  32.                 foreach (KeyValuePair<char, int> third in values)
  33.                 {
  34.                     foreach (KeyValuePair<char, int> fourth in values)
  35.                     {
  36.                         firstSequence = Convert.ToString(first.Key) + second.Key + third.Key + fourth.Key;
  37.                         firstSum = first.Value + second.Value + third.Value + fourth.Value;
  38.  
  39.                         // end first 4 digits
  40.                         foreach (KeyValuePair<char, int> fifth in values)
  41.                         {
  42.                             foreach (KeyValuePair<char, int> sixth in values)
  43.                             {
  44.                                 foreach (KeyValuePair<char, int> seventh in values)
  45.                                 {
  46.                                     foreach (KeyValuePair<char, int> eighth in values)
  47.                                     {
  48.                                         secondSequence = Convert.ToString(fifth.Key) + sixth.Key + seventh.Key + eighth.Key;
  49.                                         secondSum = fifth.Value + sixth.Value + seventh.Value + eighth.Value;
  50.  
  51.                                         if (Math.Abs(firstSum - secondSum) == diff)
  52.                                         {
  53.                                             Console.WriteLine(firstSequence + secondSequence);
  54.                                             found = true;
  55.                                         }
  56.                                     }
  57.                                 }
  58.                             }
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.         if (found == false)
  65.         {
  66.             Console.WriteLine("No");
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement