Advertisement
Guest User

Exam Problem 04 [Magic Strings] - V1

a guest
Jun 2nd, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.98 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class MagicStrings
  5. {
  6.     static void Main()
  7.     {
  8.         int diff = int.Parse(Console.ReadLine());
  9.         bool found = false;
  10.         List<string> results = new List<string>();
  11.  
  12.         for (int i = 1111; i <= 5555; i++)
  13.         {
  14.             int sum1 = Calc(i);
  15.             string seq1 = Convert.ToString(i);
  16.  
  17.             if (Check(seq1))
  18.             {
  19.                 for (int j = 1111; j <= 5555; j++)
  20.                 {
  21.                     int sum2 = Calc(j);
  22.                     string seq2 = Convert.ToString(j);
  23.  
  24.                     if (Check(seq2))
  25.                     {
  26.                         if (Math.Abs(sum1 - sum2) == diff)
  27.                         {
  28.                             seq1 = seq1.Replace('1', 'k').Replace('3', 's').Replace('4', 'n').Replace('5', 'p');
  29.                             seq2 = seq2 = seq2.Replace('1', 'k').Replace('3', 's').Replace('4', 'n').Replace('5', 'p');
  30.                             results.Add(seq1 + seq2);
  31.                             found = true;
  32.                         }
  33.                     }
  34.                 }
  35.             }
  36.         }
  37.  
  38.         if (!found)
  39.         {
  40.             Console.WriteLine("No");
  41.         }
  42.         else
  43.         {
  44.             string[] finalOutput = results.ToArray();
  45.             Array.Sort(finalOutput);
  46.  
  47.             foreach (var item in finalOutput)
  48.             {
  49.                 Console.WriteLine(item);
  50.             }
  51.         }
  52.     }
  53.  
  54.     private static int Calc(int n)
  55.     {
  56.         int sum = 0;
  57.         while (n != 0)
  58.         {
  59.             int remainder;
  60.             n = Math.DivRem(n, 10, out remainder);
  61.             sum += remainder;
  62.         }
  63.         return sum;
  64.     }
  65.  
  66.     private static bool Check(string b)
  67.     {
  68.         for (int i = 0; i < b.Length; i++)
  69.         {
  70.             if (b[i] == '2' || b[i] == '0' || b[i] > '5')
  71.             {
  72.                 return false;
  73.             }
  74.         }
  75.         return true;
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement