fbinnzhivko

04.00 Nine Digit Magic NUmber

Apr 27th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.10 KB | None | 0 0
  1. using System;
  2. class NineDigitMagicNumbers
  3. {
  4.     static void Main()
  5.     {
  6.         int sum = int.Parse(Console.ReadLine());
  7.         int diff = int.Parse(Console.ReadLine());
  8.         bool foundSolution = false;
  9.         for (int abc = 111; abc <= 777; abc++)
  10.         {
  11.             int tempoSum = 0;
  12.             int def = abc + diff;
  13.             int ghi = def + diff;
  14.  
  15.             if (ghi <= 777)
  16.             {
  17.                 string number = ("" + abc + def + ghi);
  18.                 number.ToCharArray();
  19.  
  20.                 for (int i = 0; i < 9; i++)
  21.                 {
  22.                     if (number[i] - '0' > 7 || number[i] - '0' < 1)
  23.                     {
  24.                         tempoSum = -1;
  25.                         break;
  26.                     }
  27.                     tempoSum += number[i] - '0';
  28.                 }
  29.                 if (tempoSum == sum)
  30.                 {
  31.                     Console.WriteLine(number);
  32.                     foundSolution = true;
  33.                 }
  34.             }
  35.         }
  36.         if (!foundSolution)
  37.         {
  38.             Console.WriteLine("No");
  39.         }
  40.     }
  41. }
Add Comment
Please, Sign In to add comment