forked from MiYazJE/Acepta-el-reto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp205.java
56 lines (43 loc) · 1.41 KB
/
p205.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
52
53
54
55
56
import java.util.Scanner;
public class p205 {
private static boolean esCapicua(String num) {
int left = 0, right = num.length() - 1;
while (left < right) {
if (num.charAt(left) != num.charAt(right))
return false;
left++; right--;
}
return true;
}
private static String getReverse(String num) {
String res = "";
for (int i = 0; i < num.length(); i++) {
res = num.charAt(i) + res;
}
return res;
}
public static void main(String[] args) {
final Scanner s = new Scanner(System.in);
boolean capicua, salir;
int iteraciones, n1, n2;
String numero;
for (int i = s.nextInt(); i > 0; i--) {
numero = s.next();
capicua = salir = false;
iteraciones = 0;
while (!salir) {
iteraciones++;
n1 = Integer.parseInt(numero);
n2 = Integer.parseInt(getReverse(numero));
numero = String.valueOf(n1 + n2);
salir = Integer.parseInt(numero) > 1_000_000_000;
if (!salir) {
capicua = esCapicua(numero);
salir = capicua;
}
}
System.out.println( (capicua) ? iteraciones + " " + numero
: "Lychrel?");
}
}
}