Number Chains -- Problem N (50 points)
Given a number N, let H be the number obtained by arranging the digits of N in descending order, and L
be the number obtained by arranging the digits in ascending order. Note that H >= L. Let D be the
number H - L, called the "difference number" obtained from N. This process can be repeated, by finding
the difference number obtained from D. A chain of different numbers is formed in this way. Eventually
one of the numbers in the chain is the same as a previous number, and from then on no new numbers will
appear. The "length of the chain" derived from a given number is the number of different numbers which
appear in the number chain described. Some numbers have a very short chain; for example, 6174 has a
chain of length I (7641 - 1467 = 6174), while 444 has a chain of length 2 (444 - 444 = 0; 0 - 0 = 0).
Input will be from the file PROBLEMN.DAT and will be a list of numbers, one per line, each with no
more than 9 digits. The list will be terminated by a line consisting of a single 0.
You must output one line for each line of input, giving the length of the number chain as described
above, for each input number. Note that the starting number is included in the length.
Example Input
123456789
1234
444
0
Example Output
2
4
2
|