Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
793 B
8
Indexable
#include <iostream>
#include <string>
using namespace std;

string lookAndSay(string s) {
    string result = "";
    int count = 1;

    for (int i = 1; i < s.size(); i++) {
        if (s[i] == s[i-1]) {
            count++;
        } else {
            result += to_string(count) + s[i-1];
            count = 1;
        }
    }
    result += to_string(count) + s[s.size()-1];

    return result;
}

string getNthTerm(int n) {
    string s = "1";
    for (int i = 1; i < n; i++) {
        s = lookAndSay(s);
    }
    return s;
}

int main() {
    int n;
    cin >> n;

    if (n >= 1 && n <= 25) {
        cout << getNthTerm(n) << endl;
    } else {
        cout << "Invalid input. n should be between 1 and 25." << endl;
    }
    
    return 0;
}