nmnikolov

04. NineDigitMagicNumbers

Jul 18th, 2014
170
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2.  
  3. class NineDigitMagicNumbers
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.         int sum = int.Parse(Console.ReadLine());
  8.         int diff = int.Parse(Console.ReadLine());
  9.  
  10.         int resultsCount = 0;
  11.         for (int num1 = 111; num1 <= 777; num1++)
  12.         {
  13.             int num2 = num1 + diff;
  14.             int num3 = num2 + diff;
  15.  
  16.             if (AllowedValues(num1) && AllowedValues(num2) && AllowedValues(num3) &&
  17.                  num3 <= 777 && (SumOfDigits(num1) + SumOfDigits(num2)
  18.                 + SumOfDigits(num3)) == sum)
  19.             {
  20.                 Console.WriteLine("{0}{1}{2}", num1, num2, num3);
  21.                 resultsCount++;
  22.             }
  23.         }
  24.         if (resultsCount == 0)
  25.             Console.WriteLine("No");
  26.     }
  27.  
  28.     private static bool AllowedValues(int num)
  29.     {
  30.         string digits = num.ToString();
  31.  
  32.         foreach (var element in digits)
  33.         {
  34.             if (element < '1' || element > '7')
  35.             {
  36.                 return false;
  37.             }
  38.         }
  39.         return true;
  40.     }
  41.  
  42.     private static int SumOfDigits(int num)
  43.     {
  44.         int sum = 0;
  45.  
  46.         while (num > 0)
  47.         {
  48.             sum += num % 10;
  49.             num = num / 10;
  50.         }
  51.         return sum;
  52.     }
  53. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×