递归解法
#include <iostream>
using namespace std;
int d, n;
string solve(int n) {
// recursion exit condition
if (n == 1) return to_string(d);
const string s = solve(n - 1);
string ans;
for (int i = 0, j = 0; j < s.length(); ++j) {
if (j == s.length() - 1 || s[j] != s[j + 1]) {
ans += s[j] + to_string(j - i + 1);
i = j + 1;
}
}
return ans;
}
int main(void) {
scanf("%d %d", &d, &n);
printf("%s\n", solve(n).c_str());
return 0;
}
请加上讲解,谢谢!