Oct 29, 2019 at 6:41pm UTC
What's Logic Behind this sequence 1, 11, 21, 1211, 111221, 312211, 13112221, 1113213211.... !?
I have to print it using loops. But not comming up with logic ?
Can someone Just help me.....
Oct 31, 2019 at 1:55am UTC
1 11 101 111011 11110101 100110111011
Oct 31, 2019 at 9:19am UTC
Yes, @Ganado!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <string>
using namespace std;
string toBinary( int num ) { return ( num > 1 ? toBinary( num / 2 ): "" ) + to_string( num % 2 ); }
string getNextTerm( const string &str )
{
int num = 1;
while ( num < str.size() && str[num] == str[0] ) num++;
return toBinary( num ) + str[0] + ( num < str.size() ? getNextTerm( str.substr( num ) ) : "" );
}
int main()
{
for ( string s = "1" ; s.size() < 200; s = getNextTerm( s ) ) cout << s << '\n' ;
}
Last edited on Oct 31, 2019 at 9:27am UTC