-
Notifications
You must be signed in to change notification settings - Fork 2
/
look_and_say.cpp
70 lines (63 loc) · 1.29 KB
/
look_and_say.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
/*The following code generates the terms of the look-and-say sequence upto 128 digits long(max 15 terms).
The first few terms of the sequence are :
1
11
21
1211
111221
312211
13112221
1113213211
31131211131221
13211311123113112211
*/
#include <iostream>
void lookNsay(int);
void display(int*);
void swap(int**, int**);
int main()
{
int terms = 0;
std::cout << "This program generates the look-and-say sequence up to a maximum of 15 terms." << std::endl;
std::cout << "Please enter the number of terms to display." << std::endl;
std::cin >> terms;
std::cout << "\n\nThe sequence is: \n\n" << std::endl;
lookNsay(terms);
}
void display(int *arr)
{
for(int i = 0; arr[i] != 0; i++)
std::cout << arr[i];
std::cout << "\n";
}
void swap(int **a, int **b)
{
int *tmp = *a;
*a = *b;
*b = tmp;
}
void lookNsay(int num)
{
int continuousSeqCount = 0, i = 0, j = 0, currentTerm = 0, terms = 0;
int *arr = new int[128]();
int *seq = new int[128]();
arr[0] = 1;
while(terms != num)
{
display(arr);
terms++;
i = j = continuousSeqCount = 0;
while(arr[i] != 0)
{
currentTerm = arr[i];
continuousSeqCount = 1;
while(arr[++i] == currentTerm)
++continuousSeqCount;
seq[j++] = continuousSeqCount;
seq[j++] = currentTerm;
}
swap(&arr, &seq);
}
delete [] arr;
delete [] seq;
}