The count-and-say sequence is the sequence of integers with the first five terms as following:
- 1
- 11
- 21
- 1211
- 111221
1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.
That means every integer (repeated continuously) is read off with its count value.
For a given n, Print the count and say sequence.
Examples
For n=0
It's a blank string
-------------------------
For n=1
Its "1"
-------------------------
For n=2, we need to evaluate the previous string which is for n=1
For n=1
"1"
So one "1"
Thus For n=2
"11"
-------------------------
Needless to say for n=3
Evaluating n=2 results in two "1"->"21"
So on...
Pre-requisite:
to_string function: In C++ we have an in-build function which converts a numerical value to its string representation. Like 1 is converted to "1" (the first 1 is of int datatype, while the second is of string datatype).
prototype of the function is:
Algorithm:
Initialize a count variable to store repeating number's frequency
While same number( character in result string actually) is repeating
Increment count
End While
Convert count value to string using the to_string function.
temp=temp+ to_string(count) + result[j] //count comes first
End FOR
C++ implementation
Output