#t51. 四位黑洞数
四位黑洞数
The Four-Digit Black Hole Number
Problem Description
The black hole number (also called trap number, or the "Kaprekar problem") is a class of numbers with a peculiar transformation property. Any four-digit number whose digits are not all the same will, after a finite number of "rearrange-and-subtract" operations, always reach . The resulting is the four-digit black hole number. ( is the three-digit black hole number.) The so-called "rearrange-and-subtract" operation means: subtract the smallest number formed by rearranging the digits from the largest number formed by rearranging the digits.
For example, for the four-digit number :
- 1st operation: ;
- 2nd operation: (note: is written as );
- 3rd operation: ;
after which it stays at the black hole number . If the four digits of the number are all the same, one operation yields .
Given any four-digit number, write a program that prints the rearrange-and-subtract process.
Input Format
A four-digit number given on one line.
Output Format
Print the rearrange-and-subtract process in the following format:
index: largestRearrangedNumber - smallestRearrangedNumber = difference
The index starts from and continues until appears on the right side of the equals sign.
Sample Input
1234
Sample Output
1: 4321 - 1234 = 3087
2: 8730 - 378 = 8352
3: 8532 - 2358 = 6174
Sample Explanation
The input four-digit number is . First rearrange the digits to get the largest number and the smallest number , subtract to get . Then repeat the operation on : rearrange to get the largest number and the smallest number (written as ), subtract to get . Continue: rearrange to get largest and smallest , subtract to get , and the process ends.
Constraints
The input four-digit number satisfies .