Advertisement
6desislava6

Problem 18.** Nine-Digit Magic Numbers

Oct 17th, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.94 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. class DigitMagic
  4. {
  5.     public static int sumDigits(int number)
  6.     {
  7.         int sameNumber = number;
  8.         int sum = 0;
  9.         for (int counter = 0; counter < 9; counter++)
  10.         {
  11.             sum += (sameNumber % 10);
  12.             sameNumber /= 10;
  13.         }
  14.         return sum;
  15.     }
  16.     public static bool Digits(int number)
  17.     {
  18.         int sameNumber = number;
  19.         bool isOk = true;
  20.         for (int counter = 0; counter < 9; counter++)
  21.         {
  22.             if ((sameNumber % 10 == 0) || (sameNumber % 10 == 8) || (sameNumber % 10 == 9))
  23.             {
  24.                 isOk = false;
  25.             }
  26.             sameNumber /= 10;
  27.         }
  28.         return isOk;
  29.     }
  30.     static void Main()
  31.     {
  32.         List<int> MagicNumbers = new List<int>();
  33.         int firstThree = new int();
  34.         int sum = int.Parse(Console.ReadLine());
  35.         int diff = int.Parse(Console.ReadLine());
  36.         int secondThree = new int();
  37.         int thirdThree = new int();
  38.         for (int first = 1; first <= 7; first++)
  39.         {
  40.             for (int second = 1; second <= 7; second++)
  41.             {
  42.                 for (int third = 1; third <= 7; third++)
  43.                 {
  44.                     firstThree = third + second * 10 + first * 100;
  45.                     secondThree = firstThree + diff;
  46.                     thirdThree = secondThree + diff;
  47.                     int wholeNumber = firstThree * 1000000 + secondThree * 1000 + thirdThree;
  48.                     if (sumDigits(wholeNumber) == sum && Digits(wholeNumber)&&firstThree<=777)
  49.                     {
  50.                         MagicNumbers.Add(wholeNumber);
  51.                     }
  52.                 }
  53.             }
  54.         }
  55.         foreach (var num in MagicNumbers)
  56.         {
  57.             Console.WriteLine(num);
  58.         }
  59.         if (MagicNumbers.Count==0)
  60.         {
  61.             Console.WriteLine("No");
  62.         }
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement