Under construction

Still being built. Things may change

Cabric Number

Time limit1sMemory limit128 MB

Summary
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.

  1. Sort the digits from large to small to build the largest number those four digits can form.
  2. 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.
  3. 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.

Examples2

  1. Example 1

    Input
    5364
    2221
    4444
    -1
    
    Expected output
    N=5364:
    6543-3456=3087
    8730-378=8352
    8532-2358=6174
    Ok!! 3 times
    N=2221:
    2221-1222=999
    999-999=0
    Ok!! 2 times
    N=4444:
    No!!
    
  2. Example 2

    Input
    6174
    1000
    -1
    
    Expected output
    N=6174:
    7641-1467=6174
    Ok!! 1 times
    N=1000:
    1000-1=999
    999-999=0
    Ok!! 2 times