svetlozar_kirkov

Hayvan Numbers (100/100)

Nov 19th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace HayvanNumbers
  6. {
  7.     class HayvanNumbers
  8.     {
  9.         static void Main()
  10.         {
  11.             int sum = int.Parse(Console.ReadLine());
  12.             int diff = int.Parse(Console.ReadLine());
  13.             int count = 0;
  14.             for (int first = 555; first <= 999; first++)
  15.             {
  16.                 int second = first + diff;
  17.                 int third = second + diff;
  18.                 List<int> individualSums = new List<int>();
  19.                 int[] firstSplit = Array.ConvertAll(first.ToString().ToArray(), x=>(int)x - 48);
  20.                 int[] secondSplit = Array.ConvertAll(second.ToString().ToArray(), x => (int)x - 48);
  21.                 int[] thirdSplit = Array.ConvertAll(third.ToString().ToArray(), x => (int)x - 48);
  22.                 if (firstSplit.Sum() + secondSplit.Sum() + thirdSplit.Sum() == sum && isValidNum(firstSplit)
  23.                     && isValidNum(secondSplit) && isValidNum(thirdSplit))
  24.                 {
  25.                     Console.Write("{0}{1}{2}",first,second,third);
  26.                     Console.WriteLine();
  27.                     count++;
  28.                 }
  29.             }
  30.             if (count==0)
  31.             {
  32.                 Console.WriteLine("No");
  33.             }
  34.         }
  35.  
  36.         static bool isValidNum(params int[] values)
  37.         {
  38.             for (int i = 0; i < values.Length; i++)
  39.             {
  40.                 if (values[i] < 5 || values[i] > 9)
  41.                 {
  42.                     return false;
  43.                 }
  44.             }
  45.             return true;
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment