Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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”.
- //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].
- //Your task is to write a program to print all Hayvan numbers for given sum and diff in increasing order.
- //Input
- //• The input data should be read from the console.
- //• The number sum stays at the first line.
- //• The number diff stays at the second line.
- //The input data will always be valid and in the format described. There is no need to check it explicitly.
- //Output
- //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”.
- //Constraints
- //• The number sum will be a positive integer number in the range [0…100].
- //• The number diff will be a positive integer number in the range [0…1000].
- //• Allowed working time for your program: 0.25 seconds.
- //• Allowed memory: 16 MB.
- using System;
- class NineDigitMagicNumber
- {
- static void Main()
- {
- int sum = int.Parse(Console.ReadLine());
- int diff = int.Parse(Console.ReadLine());
- bool isNum = false;
- for (int i = 111; i <= 777; i++)
- {
- int abc = i;
- int def = abc + diff;
- int ghi = def + diff;
- if (ghi>777)
- {
- break;
- }
- string wholeNumber = "" + abc + def + ghi;
- if (wholeNumber.Contains("8")||wholeNumber.Contains("9")||wholeNumber.Contains("0"))
- {
- continue;
- }
- int targetSum = 0;
- for (int k = 0; k < wholeNumber.Length; k++)
- {
- targetSum += Convert.ToInt32(Convert.ToString(wholeNumber[k]));
- }
- if (targetSum==sum)
- {
- Console.WriteLine(wholeNumber);
- isNum = true;
- }
- }
- if (!isNum)
- {
- Console.WriteLine("No");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement