-
Notifications
You must be signed in to change notification settings - Fork 7
/
Main.java
51 lines (42 loc) · 1.03 KB
/
Main.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
44
45
46
47
48
49
50
51
package basicLevel1019;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int number = in.nextInt();
in.close();
while (true) {
int d = desc(number);
int i = incr(number);
number = i - d;
if (number == 0) {
System.out.printf("%04d - %04d = %04d\n", i, d, number);
break;
}
if (number == 6174) {
System.out.printf("%04d - %04d = %04d\n", i, d, number);
break;
}
System.out.printf("%04d - %04d = %04d\n", i, d, number);
}
}
public static int desc(int number) {
char[] array = String.format("%04d", number).toCharArray();
Arrays.sort(array);
int temp = 0;
for (int i = 0; i < 4; i++) {
temp = temp * 10 + (array[i] - '0');
}
return temp;
}
public static int incr(int number) {
char[] array = String.format("%04d", number).toCharArray();
Arrays.sort(array);
int temp = 0;
for (int i = 3; i >= 0; i--) {
temp = temp * 10 + (array[i] - '0');
}
return temp;
}
}