水题
#include<bits/stdc++.h>
using namespace std;
int n;
char s[35][10000];
int main() {
//scanf("%d", &n);
s[0][0] = '1';
s[1][0]=s[1][1]='1';
//第n个串就是第n-1个的读法,第一个应设置为11,
while (scanf("%d", &n)) {
for (int i = 2; i < n; i++) {
int cnt = 1;
char x = s[i - 1][0];
for (int j = 1, y = 0; j<strlen(s[i - 1]); j++) {
if (x == s[i - 1][j]) {
cnt++;
}
else if (x != s[i - 1][j]) {
while (cnt > 0) {
char z = cnt % 10 + '0';
cnt /= 10;
s[i][y++] = z;
}
s[i][y++] = x;
x = s[i - 1][j];
cnt = 1;
}
if (j == strlen(s[i - 1]) - 1) {
while (cnt > 0) {
char z = cnt % 10 + '0';
cnt /= 10;
s[i][y++] = z;
}
s[i][y++] = x;
}
}
}
printf("%s\n", s[n - 1]);
}
return 0;
}
本文介绍了一道简单的编程题目,通过使用C++实现一种特定的字符串生成算法。该算法根据前一个字符串生成下一个字符串,具体规则是连续相同的字符被计数并跟随其后的数字表示。文章提供了一个完整的代码示例,演示了如何从初始字符串“11”开始生成指定位置的字符串。
230

被折叠的 条评论
为什么被折叠?



