VyaraG

NineDigitMagicNumbers

Dec 1st, 2014
328
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Hayvan often plays with numbers. His recent game was to play with 9-digit numbers and calculate their sums of digits, as well as to split them into triples with special properties. Help Hayvan to find a very special set of numbers called “Hayvan numbers”.
  2. //Hayvan numbers are 9-digit numbers in format abcdefghi, such that their sub-numbers abc, def and ghi have a difference diff (ghi-def = def-abc = diff), their sum of digits is sum and abc < def < ghi, where each digit a, b, c, d, e, f, g, h and i is in range [5…9].
  3. //Your task is to write a program to print all Hayvan numbers for given sum and diff in increasing order.
  4. //Input
  5. //•   The input data should be read from the console.
  6. //•   The number sum stays at the first line.
  7. //•   The number diff stays at the second line.
  8. //The input data will always be valid and in the format described. There is no need to check it explicitly.
  9. //Output
  10. //The output should be printed on the console. Print Hayvan numbers matching given difference diff and given sum of digits sum, in increasing order, each at a separate line. In case no Hayvan numbers exits, print “No”.
  11. //Constraints
  12. //•   The number sum will be a positive integer number in the range [0…100].
  13. //•   The number diff will be a positive integer number in the range [0…1000].
  14. //•   Allowed working time for your program: 0.25 seconds.
  15. //•   Allowed memory: 16 MB.
  16.  
  17.  
  18. using System;
  19.  
  20. class NineDigitMagicNumber
  21. {
  22.     static void Main()
  23.     {
  24.  
  25.         int sum = int.Parse(Console.ReadLine());
  26.         int diff = int.Parse(Console.ReadLine());
  27.         bool isNum = false;
  28.  
  29.         for (int i = 111; i <= 777; i++)
  30.         {
  31.             int abc = i;
  32.             int def = abc + diff;
  33.             int ghi = def + diff;
  34.  
  35.             if (ghi>777)
  36.             {
  37.                 break;
  38.             }
  39.             string wholeNumber = "" + abc + def + ghi;
  40.             if (wholeNumber.Contains("8")||wholeNumber.Contains("9")||wholeNumber.Contains("0"))
  41.             {
  42.                 continue;
  43.             }
  44.             int targetSum = 0;
  45.             for (int k = 0; k < wholeNumber.Length; k++)
  46.             {
  47.                 targetSum += Convert.ToInt32(Convert.ToString(wholeNumber[k]));
  48.             }
  49.             if (targetSum==sum)
  50.             {
  51.                 Console.WriteLine(wholeNumber);
  52.                 isNum = true;
  53.             }
  54.         }
  55.         if (!isNum)
  56.         {
  57.             Console.WriteLine("No");
  58.         }
  59.     }
  60. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×