Advertisement
fbinnzhivko

04.00 Hayvan Numbers

May 2nd, 2016
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. using System;
  2. class HayvanNumbers
  3. {
  4.     static void Main()
  5.     {
  6.         int sum = int.Parse(Console.ReadLine());
  7.         int diff = int.Parse(Console.ReadLine());
  8.         int resultsCount = 0;
  9.         for (int num1 = 555; num1 <= 999; num1++)
  10.         {
  11.             int num2 = num1 + diff;
  12.             int num3 = num2 + diff;
  13.             if (IsAllowedNumber(num1) && IsAllowedNumber(num2)
  14.                 && IsAllowedNumber(num3) && (num3 <= 999) &&
  15.                 CalcSumOfDigits(num1) + CalcSumOfDigits(num2) + CalcSumOfDigits(num3) == sum)
  16.             {
  17.                 Console.WriteLine("{0}{1}{2}", num1, num2, num3);
  18.                 resultsCount++;
  19.             }
  20.         }
  21.         if (resultsCount == 0)
  22.         {
  23.             Console.WriteLine("No");
  24.         }
  25.     }
  26.     private static int CalcSumOfDigits(int num)
  27.     {
  28.         int sum = 0;
  29.         while (num > 0)
  30.         {
  31.             sum += num % 10;
  32.             num = num / 10;
  33.         }
  34.         return sum;
  35.     }
  36.     private static bool IsAllowedNumber(int num)
  37.     {
  38.         string digits = num.ToString();
  39.         foreach (var digit in digits)
  40.         {
  41.             if (digit < '5' || digit > '9')
  42.             {
  43.                 return false;
  44.             }
  45.         }
  46.         return true;
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement