-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1334.cpp
50 lines (41 loc) · 1.09 KB
/
1334.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
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(){
ios::sync_with_stdio(false); cin.tie(NULL);
int N; cin >> N;
if(N < 9) cout << N + 1;
else if(N == 9) cout << 11;
else{
string str = to_string(N);
int midIdx = 0;
if(N % 2 != 0) midIdx = str.length() / 2 + 1;
else midIdx = str.length() / 2;
string afterPalin = str;
string palin = str.substr(0, midIdx);
reverse(palin.begin(), palin.end());
afterPalin.replace(afterPalin.length() - palin.length(), palin.length(), palin);
if(afterPalin > str){
cout << afterPalin;
return 0;
}
else{
// 9로만 이루어졌는지 확인
string palin2 = str.substr(0, midIdx);
string isNine(palin2.length(), '9');
if(isNine == palin2){
string toZero(str.length() - 1, '0');
toZero = '1' + toZero + '1';
cout << toZero;
return 0;
}
palin2 = to_string(stoi(palin2) + 1);
str.replace(0, palin2.length(), palin2);
reverse(palin2.begin(), palin2.end());
str.replace(str.length() - palin2.length(), palin2.length(), palin2);
cout << str;
}
}
return 0;
}