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
|
/*
Challenge 116
TASK #1 - Number Sequence
Submitted by: Mohammad S Anwar
You are given a number $N >= 10.
Write a script to split the given number such that the difference between two
consecutive numbers is always 1 and it shouldn't have leading 0.
Print the given number if it impossible to split the number.
Example
Input: $N = 1234
Output: 1,2,3,4
Input: $N = 91011
Output: 9,10,11
Input: $N = 10203
Output: 10203 as it is impossible to split satisfying the conditions.
*/
#include <cassert>
#include <iostream>
#include <string>
using namespace std;
bool found_solution = false;
void print_sequences(int prev, const string& seq, const string& rest) {
if (rest.empty()) { // consumed all rest
if (!found_solution) { // only first solution printed
cout << seq << endl;
found_solution = true;
}
}
else {
for (int i = 1; !found_solution && i <= static_cast<int>(rest.size()); i++) {
string prefix = rest.substr(0, i);
string suffix = rest.substr(i);
int new_prev = atoi(prefix.c_str());
if (suffix[0] != '0') {
if (prev < 0) { // first time
string new_seq = prefix;
if (!found_solution)
print_sequences(new_prev, new_seq, suffix);
}
else {
if (prev + 1 == new_prev) {
string new_seq = seq + "," + prefix;
if (!found_solution)
print_sequences(new_prev, new_seq, suffix);
}
}
}
}
}
}
int main(int argc, char* argv[]) {
if (argc != 2) return EXIT_FAILURE;
print_sequences(-1, "", argv[1]);
}
|