-
Notifications
You must be signed in to change notification settings - Fork 0
/
palin.cpp
99 lines (97 loc) · 2.16 KB
/
palin.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
int main(void)
{
int flag, nine;
long int testCases, start, len;
scanf("%ld", &testCases);
for (int i=0; i<testCases; i++)
{
flag = 0;
nine = 0;
string number;
cin >> number;
len = strlen(number.c_str());
char initial[len];
strcpy(initial, number.c_str());
(len%2 == 0) ? start = len/2-1 : start = len/2;
for (long int j=start; j>=0; j--)
{
if (flag == 0)
{
if (number[j] != '9' || number[len - j - 1] != '9')
{
flag = 1;
if (len % 2 != 0) //ODD
{
if (number[j] >= number[len-j-1] && nine == 0)
number[len - j - 1] = number[j];
if (number[j] != '9' && nine == 0)
if (number[j] < number[len-j-1])
number[len - j - 1] = ++number[j];
if (nine == 1)
{
if (number[j] != '9')
{
for (int k=j+1; k<=start; k++)
number[k] = number[len-k-1] = '0';
number[len - j - 1] = ++number[j];
}
else
if (number[j] == number[len-j-1])
flag = 0;
else
number[len - j - 1] = number[j];
}
}
else //EVEN
{
if (number[j] >= number[len-j-1] && nine == 0)
number[len - j - 1] = number[j];
if (number[j] != '9' && nine == 0)
if (number[j] < number[len-j-1])
number[len - j - 1] = ++number[j];
if (nine == 1)
{
if (number[j] != '9')
{
for (int k=j+1; k<=start; k++)
number[k] = number[len-k-1] = '0';
number[len - j - 1] = ++number[j];
}
else
if (number[j] == number[len-j-1])
flag = 0;
else
number[len - j - 1] = number[j];
}
}
}
else
nine = 1;
}
else
number[len-j-1] = number[j];
}
if (flag == 0)
{
for (long int j=start; j>=0; j--)
number[j] = number[len-j-1] = '0';
}
if (strcmp(initial, number.c_str()) == 0)
{
if (len%2 == 0)
number[len - start - 1] = ++number[start];
else
++number[start];
}
if (flag == 0)
printf("1%s\n", number.c_str());
else
printf("%s\n", number.c_str());
}
return 0;
}