分析
遍历序列,记录 连续 相同的数字
生成下一个序列(记录 连续 相同的数字)
直到递归次数为n
具体来说
- 递归
由于是参照上一个序列,生成下一个序列,同时作为下一次递归的“参照”
所以需要2个vector,递归时交换次序
- 搜索
由于数字是固定【0,9】,因此做个数表,找连续 相同的数字,就从数表里搜索
其他注意
由于不知道最终的序列长度是多少,因此设为尽可能长
在测试下,至少要100000(反正我从1000开始测试,测试点6过不去,100000可以)
代码
#include<bits/stdc++.h>
using namespace std;
int key, length, times{ 1 };
vector<int>T{ 0,1,2,3,4,5,6,7,8,9 };//固定常数(表)用大写
void InPut(vector<int>&, vector<int>&);
void Recursive(vector<int>, vector<int>);
int main() {
vector<int>table, table2;
InPut(table, table2);
Recursive(table, table2);
return 0;
}
void InPut(vector<int>& table, vector<int>& table2) {
scanf("%d %d", &key, &length);
table.resize(100000);
table2.resize(100000);
fill(table.begin(), table.end(), -1);
fill(table2.begin(), table2.end(), -1);
table[0] = key;
}
void Recursive(vector<int> table, vector<int> table2) {
if (times == length) {
for (auto i : table) {
if (i != -1)
printf("%d", i);
}
return;
}
times++;
int count, j, z{ 0 };
for (int i = 0; i < table.size() && table[i] != -1; i = j) {
count = 0;
auto it = find(T.begin(), T.end(), table[i]);
for (j = i; j < table.size() && table[j] == *it; j++) {
count++;
}
table2[z] = *it;
z++;
table2[z] = count;
z++;
}
Recursive(table2, table);
}
后记
这种解法确实很一般,甚至很糟糕;是没有柳神的string来的简洁的
仅是自己做时的第一反应,刚好也能应付这题