Guest User

MagicStrings

a guest
Apr 15th, 2014
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.46 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 ConsoleApplication2
  8. {
  9.     class Program
  10.     {
  11.         static int GetWeight(string s)
  12.         {
  13.             int result = 0;
  14.             for (int i = 0; i < s.Length; i++)
  15.             {
  16.                 result += weights[s[i]];
  17.             }
  18.             return result;
  19.         }
  20.  
  21.         static Dictionary<char, int> weights = new Dictionary<char, int>();
  22.  
  23.         static void Main(string[] args)
  24.         {
  25.            
  26.             weights.Add('s', 3);
  27.             weights.Add('n', 4);
  28.             weights.Add('k', 1);
  29.             weights.Add('p', 5);
  30.             char[] arr = {'s','n','k','p'};
  31.             int diff = int.Parse(Console.ReadLine());
  32.             StringBuilder sample = new StringBuilder();
  33.             List<string> all = new List<string>();
  34.             for (int i = 0; i < 4; i++)
  35.             {
  36.                 for (int j = 0; j < 4; j++)
  37.                 {
  38.                     for (int k = 0; k < 4; k++)
  39.                     {
  40.                         for (int p = 0; p < 4; p++)
  41.                         {
  42.                             sample.Append(arr[i]);
  43.                             sample.Append(arr[j]);
  44.                             sample.Append(arr[k]);
  45.                             sample.Append(arr[p]);
  46.                             all.Add(sample.ToString());
  47.                             sample.Clear();
  48.                         }
  49.                     }  
  50.                 }  
  51.             }
  52.             StringBuilder result = new StringBuilder();
  53.             List<string> final = new List<string>();
  54.             for (int i = 0; i < all.Count; i++)
  55.             {
  56.                 for (int j = 0; j < all.Count; j++)
  57.                 {
  58.                     if (Math.Abs(GetWeight(all[i]) - GetWeight(all[j])) == diff)
  59.                     {
  60.                         result.Append(all[i]);
  61.                         result.Append(all[j]);
  62.                         final.Add(result.ToString());
  63.                         result.Clear();
  64.                     }
  65.                 }
  66.             }
  67.             if(final.Count == 0)
  68.             {
  69.                 Console.WriteLine("No");
  70.             }
  71.             else
  72.             {
  73.                 final.Sort();
  74.                 for (int i = 0; i < final.Count; i++)
  75.                 {
  76.                     Console.WriteLine(final[i]);
  77.                 }
  78.             }
  79.         }
Advertisement
Add Comment
Please, Sign In to add comment