Advertisement
fbinnzhivko

04. Hayvan Numbers

Apr 6th, 2016
96
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.  
  3. class HayvanNumbers
  4. {
  5.     static void Main()
  6.     {
  7.         int sum = int.Parse(Console.ReadLine());
  8.         int diff = int.Parse(Console.ReadLine());
  9.         int resultsCount = 0;
  10.         for (int num1 = 555; num1 <= 999; num1++)
  11.         {
  12.             int num2 = num1 + diff;
  13.             int num3 = num2 + diff;
  14.             if (IsAllowedNumber(num1) && IsAllowedNumber(num2) && IsAllowedNumber(num3) &&
  15.                 (num3 <= 999) &&
  16.                 CalcSumOfDigits(num1) + CalcSumOfDigits(num2) + CalcSumOfDigits(num3) == sum)
  17.             {
  18.                 Console.WriteLine("{0}{1}{2}", num1, num2, num3);
  19.                 resultsCount++;
  20.             }
  21.         }
  22.         if (resultsCount == 0)
  23.         {
  24.             Console.WriteLine("No");
  25.         }
  26.     }
  27.     private static int CalcSumOfDigits(int num)
  28.     {
  29.         int sum = 0;
  30.         while (num > 0)
  31.         {
  32.             sum += num % 10;
  33.             num = num / 10;
  34.         }
  35.         return sum;
  36.     }
  37.     private static bool IsAllowedNumber(int num)
  38.     {
  39.         string digits = num.ToString();
  40.         foreach (var digit in digits)
  41.         {
  42.             if (digit < '5' || digit > '9')
  43.             {
  44.                 return false;
  45.             }
  46.         }
  47.         return true;
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement