Last active
November 22, 2023 19:59
-
-
Save juanfal/8eb641f34ee932b0ee52d227fd80d1a1 to your computer and use it in GitHub Desktop.
look and say
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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