#P1532. 卡布列克圆舞曲

卡布列克圆舞曲

Problem 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 61746174, which is the Kaprekar constant. For example:

4321−1234=30874321-1234=3087.

8730−378=83528730-378=8352.

8532−2358=61748532-2358=6174.

7641−1467=61747641-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 5432154321:

54321−12345=4197654321-12345=41976.

97641−14679=8296297641-14679=82962.

98622−22689=7593398622-22689=75933.

97533−33579=6395497533-33579=63954.

96543−34569=6197496543-34569=61974.

97641−14679=8296297641-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 nn for which to find the "Kaprekar Waltz" (n<231n<2^{31}).

Output Format

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

4321
54321

6174
82962 75933 63954 61974

Hint

Translated by ChatGPT 5