Cabric Number
Time limit1sMemory limit128 MB
Simulate the Kaprekar digit routine to 6174 or 0 for each input and print every subtraction step.
- Level
Easy3 of 10
- Topics
- Simulation, Sorting
- Solved
- No attempts yet
Problem
Take a four digit integer N whose four digits are not all the same, then repeat the following steps.
- Sort the digits from large to small to build the largest number those four digits can form.
- Sort the digits from small to large to build the smallest number those four digits can form. If a zero is among the digits, the leading zeros fall away and the number can be shorter than four digits.
- Subtract the smaller number from the larger one. The difference becomes the next number.
Repeating this always reaches 6174 or 0, and the process stops the moment one of them appears.
When the next number is shorter than four digits, use it exactly as it is written in decimal. Do not pad it back to four digits with zeros. For 999 the largest and the smallest number are both 999, so the difference is 0.
Write a program that runs this algorithm.
Input
The input has several lines and each line holds one integer. Reading -1 ends the input, and -1 itself is not processed.
Output
For every input integer N other than -1, first print one line in the form N=<N>:.
If N has exactly four digits and they are not all the same, print one line per iteration in the form <larger>-<smaller>=<difference>, then print Ok!! <count> times as the last line, where count is the number of iterations. Write times even when the count is 1.
For any other N, print No!! and nothing else.