forked from improper4/uva
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UVa00263_NumberChains.java
43 lines (37 loc) · 1.09 KB
/
UVa00263_NumberChains.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package uva;
/* USER: 46724 (sfmunera) */
/* PROBLEM: 199 (263 - Number Chains) */
/* SUBMISSION: 08961021 */
/* SUBMISSION TIME: 2011-06-17 14:51:04 */
/* LANGUAGE: 2 */
import java.util.*;
public class UVa00263_NumberChains {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (true) {
int n = in.nextInt();
if (n == 0)
break;
System.out.println("Original number was " + n);
Set<Integer> chain = new HashSet<Integer>();
while (true) {
char[] sinc = String.valueOf(n).toCharArray();
char[] sdec = new char[sinc.length];
Arrays.sort(sinc);
for (int i = 0; i < sinc.length; ++i)
sdec[sinc.length - i - 1] = sinc[i];
int inc = Integer.parseInt(String.valueOf(sinc));
int dec = Integer.parseInt(String.valueOf(sdec));
int res = dec - inc;
System.out.println(dec + " - " + inc + " = " + res);
if (chain.contains(res)) {
System.out.println("Chain length " + (chain.size() + 1));
System.out.println();
break;
}
chain.add(res);
n = res;
}
}
}
}