蓝桥杯 14. 奇怪的数列
奇怪的数列
原题目链接
题目描述
从 X 星截获一份电码,是一些数字,如下:
13
1113
3113
132113
1113122113
⋯⋯
YY 博士经彻夜研究,发现了规律:
- 第一行的数字随便是什么,以后每一行都是对上一行 “读出来”。
- 例如:
- 第 2 行是对第 1 行的描述,意思是:1 个 1,1 个 3,所以是:1113
- 第 3 行是对第 2 行的描述,意思是:3 个 1,1 个 3,所以是:3113
你的任务是:从初始数字开始,连续进行这样的变换。
输入描述
- 第一行输入一个数字组成的字符串,不超过 100 位。
- 第二行输入一个数字
n
,表示需要连续变换多少次,n
不超过 20。
输出描述
输出一个字符串,表示最后一次变换完的结果。
输入输出样例
输入
5
7
输出
13211321322115
c++代码
#include<bits/stdc++.h>using namespace std;string change(string str) {int i = 0;string ans;while(i < str.size()) {int cont = 0;while(i + cont < str.size() && str[i + cont] == str[i]) cont++;ans += to_string(cont) + str[i];i = i + cont;}return ans;
}int main() {string a;int b;cin >> a >> b;while(b--) a = change(a);cout << a;return 0;
}//by wqs