Skip to content

Instantly share code, notes, and snippets.

@juanfal
Last active November 22, 2023 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanfal/8eb641f34ee932b0ee52d227fd80d1a1 to your computer and use it in GitHub Desktop.
Save juanfal/8eb641f34ee932b0ee52d227fd80d1a1 to your computer and use it in GitHub Desktop.
look and say
// t11e24.lookandsay.cpp
// juanfc 2023-11-22
//
#include <iostream>
using namespace std;
string lookandsay(string s);
int main()
{
string previous = "1";
do {
string newone = lookandsay(previous);
cout << previous << endl;
previous = newone;
} while (previous.length() <= 20);
return 0;
}
int reps(string s, int pos);
string lookandsay(string s)
{
string r;
int i = 0;
while (i < s.length()) {
int cont = reps(s, i);
r += char(cont + '0');
r += s[i];
i += cont;
}
return r;
}
int reps(string s, int pos)
{
int i = 0;
while (pos+i < s.length() and s[pos+i] == s[pos])
++i;
return i;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment