Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- class NineDigitMagicNumbers
- {
- static void Main(string[] args)
- {
- // input
- int sum = int.Parse(Console.ReadLine());
- int diff = int.Parse(Console.ReadLine());
- // declarations
- IEnumerable<string> combinations = CombinationsWithRepition("1234567", 3);
- List<string> workCopy = combinations.ToList();
- List<int> numbers = workCopy.Select(int.Parse).ToList();
- List<string> result = new List<string>();
- // logic
- for (int i = 0; i < numbers.Count; i++)
- {
- int temp1 = numbers[i] + diff;
- if (numbers.Contains(temp1))
- {
- int temp2 = temp1 + diff;
- if (numbers.Contains(temp2))
- {
- int tempSum = 0;
- for (int l = 1; l <= 3; l++)
- {
- tempSum += GetDigit(numbers[i], l);
- tempSum += GetDigit(temp1, l);
- tempSum += GetDigit(temp2, l);
- }
- if (tempSum == sum)
- {
- string x = workCopy[i] + temp1.ToString() + temp2.ToString();
- result.Add(x);
- }
- }
- }
- }
- // printing
- if (result.Count > 0)
- {
- foreach (var item in result)
- {
- Console.WriteLine(item);
- }
- }
- else
- {
- Console.WriteLine("No");
- }
- }
- #region Create all combinations with repetitions 7 choose 3
- static String Convert(string symbols, int number, int totalLen)
- {
- var result = "";
- var len = symbols.Length;
- var nullSym = symbols[0];
- while (number > 0)
- {
- var index = number % len;
- number = number / len;
- result = symbols[index] + result;
- }
- return result.PadLeft(totalLen, nullSym);
- }
- static IEnumerable<String> CombinationsWithRepition(string symbols, int len)
- {
- for (var i = 0; i < Math.Pow(symbols.Length, len); i++)
- yield return Convert(symbols, i, len);
- }
- #endregion
- // GetDigit method - splits an int into digits
- static int GetDigit(int number, int digit)
- {
- return (number / (int)Math.Pow(10, digit - 1)) % 10;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement