Advertisement
Guest User

Nine-Digit Magic Numbers

a guest
Apr 9th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7.     static void Main(string[] args)
  8.     {
  9.         // input
  10.         int sum = int.Parse(Console.ReadLine());
  11.         int diff = int.Parse(Console.ReadLine());
  12.  
  13.         // declarations
  14.         IEnumerable<string> combinations = CombinationsWithRepition("1234567", 3);
  15.         List<string> workCopy = combinations.ToList();
  16.         List<int> numbers = workCopy.Select(int.Parse).ToList();
  17.         List<string> result = new List<string>();
  18.  
  19.         // logic
  20.         for (int i = 0; i < numbers.Count; i++)
  21.         {
  22.             int temp1 = numbers[i] + diff;
  23.             if (numbers.Contains(temp1))
  24.             {
  25.                 int temp2 = temp1 + diff;
  26.                 if (numbers.Contains(temp2))
  27.                 {
  28.  
  29.                     int tempSum = 0;
  30.                     for (int l = 1; l <= 3; l++)
  31.                     {
  32.                         tempSum += GetDigit(numbers[i], l);
  33.                         tempSum += GetDigit(temp1, l);
  34.                         tempSum += GetDigit(temp2, l);
  35.                     }
  36.  
  37.                     if (tempSum == sum)
  38.                     {
  39.                         string x = workCopy[i] + temp1.ToString() + temp2.ToString();
  40.                         result.Add(x);
  41.                     }
  42.                 }
  43.             }
  44.         }
  45.  
  46.         // printing
  47.         if (result.Count > 0)
  48.         {
  49.             foreach (var item in result)
  50.             {
  51.                 Console.WriteLine(item);
  52.             }
  53.         }
  54.         else
  55.         {
  56.             Console.WriteLine("No");
  57.         }
  58.     }
  59.  
  60.     #region Create all combinations with repetitions 7 choose 3
  61.     static String Convert(string symbols, int number, int totalLen)
  62.     {
  63.         var result = "";
  64.         var len = symbols.Length;
  65.         var nullSym = symbols[0];
  66.         while (number > 0)
  67.         {
  68.             var index = number % len;
  69.             number = number / len;
  70.             result = symbols[index] + result;
  71.         }
  72.         return result.PadLeft(totalLen, nullSym);
  73.     }
  74.  
  75.     static IEnumerable<String> CombinationsWithRepition(string symbols, int len)
  76.     {
  77.         for (var i = 0; i < Math.Pow(symbols.Length, len); i++)
  78.             yield return Convert(symbols, i, len);
  79.     }
  80.  
  81.     #endregion
  82.  
  83.     // GetDigit method - splits an int into digits
  84.     static int GetDigit(int number, int digit)
  85.     {
  86.         return (number / (int)Math.Pow(10, digit - 1)) % 10;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement