-
Notifications
You must be signed in to change notification settings - Fork 13
/
next-growing-number.cpp
63 lines (50 loc) · 1.25 KB
/
next-growing-number.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
#include <iostream>
#include <string>
std::string nextGrowing( std::string p_num ) {
for ( int c1 = p_num.size() -1; c1 >= 0; c1-- ) {
if ( p_num[c1] != '9' ) {
p_num[c1]++;
for ( int c2 = c1 + 1; c2 < p_num.size(); c2++ ) {
p_num[c2] = '0';
}
break;
}
if ( c1 == 0 ) {
for ( int c2 = 0; c2 < p_num.size(); c2++ ) {
p_num[c2] = '0';
}
p_num.insert(0, 1, '1');
}
}
char l_cur{ p_num[0] };
bool l_did{ false };
for ( auto& c : p_num ) {
if ( c < l_cur || l_did ) { c = l_cur; l_did = true; }
l_cur = c;
}
return p_num;
}
int main()
{
std::string N;
std::getline(std::cin, N);
std::cout << nextGrowing(N) << std::endl;
}
/*
// "Cheating" solution
// i.e. Converting input string to integer
#include <iostream>
#include <string>
#include <algorithm>
int main() {
unsigned long long n;
std::cin >> n;
n++;
auto s = std::to_string(n);
auto f = std::adjacent_find(std::begin(s), std::end(s),
[](auto a, auto b){ return a>b; });
if (f != std::end(s))
std::fill(std::next(f), std::end(s), *f);
std::cout << s << std::endl;
}
*/