-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPALIN_2011-11-06_15;54;15.java
67 lines (65 loc) · 1.75 KB
/
PALIN_2011-11-06_15;54;15.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
57
58
59
60
61
62
63
64
65
66
67
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Arrays;
public class Main {
static char[] rev(char[] numb) {
int len = numb.length;
int mid = len / 2 - (len + 1) % 2;
char[] dummy = Arrays.copyOf(numb, len);
for (int i = 0; i <= mid; i++) {
dummy[len - 1 - i] = dummy[i];
}
return dummy;
}
public static void main(String[] args) throws IOException {
final FileOutputStream fdout = new FileOutputStream(FileDescriptor.out);
final BufferedOutputStream bos = new BufferedOutputStream(fdout, 1024);
final PrintStream ps = new PrintStream(bos, false);
System.setOut(ps);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
for (int i = 0; i < t; i++) {
char[] numb = in.readLine().toCharArray();
int mid = numb.length / 2 - (numb.length + 1) % 2;
char[] rn = rev(numb);
boolean doIt = false;
for (int j = mid+1; j < numb.length; j++) {
if (numb[j] < rn[j]) {
doIt = true;
break;
} else if (numb[j] > rn[j]) {
break;
}
}
if (doIt) {
System.out.println(new String(rn));
} else {
boolean prob = true;
for (int j = mid; j >= 0 && prob; j--) {
if (numb[j] == '9')
numb[j] = '0';
else {
numb[j]++;
prob = false;
}
}
if (prob) {
char[] nb = new char[numb.length + 1];
nb[0] = '1';
for (int j = 0; j < numb.length; j++) {
nb[j + 1] = numb[j];
}
System.out.println(new String(rev(nb)));
continue;
}
System.out.println(new String(rev(numb)));
}
}
ps.close();
}
}