P1532 Kaprekar Waltz

Description

Kaprekar was a mathematician. He discovered that for any four-digit number not composed of completely identical digits: if you reorder its digits to form the largest number and the smallest number, then subtract the smaller from the larger; if the difference has fewer than four digits, pad with leading zeros; and repeat this process, it will eventually become a fixed number $6174$, which is the Kaprekar constant. For example: $4321-1234=3087$. $8730-378=8352$. $8532-2358=6174$. $7641-1467=6174$. If a K-digit number is processed in the same way, it does not become a single number but forms a cycle among several numbers, called the Kaprekar Waltz. For example, for the five-digit number $54321$: $54321-12345=41976$. $97641-14679=82962$. $98622-22689=75933$. $97533-33579=63954$. $96543-34569=61974$. $97641-14679=82962$. We call 82962, 75933, 63954, 61974 the repeating cycle, i.e., the Kaprekar Waltz.

Input Format

Multiple lines. Each line contains a starting integer $n$ for which to find the "Kaprekar Waltz" ($n

Output Format

For each input integer, output the corresponding cycle terms on one line, separated by spaces.

Explanation/Hint

Translated by ChatGPT 5