题目描述
从X星截获一份电码,是一些数字,如下:
13
1113
3113
132113
1113122113
…
YY博士经彻夜研究,发现了规律:
第一行的数字随便是什么,以后每一行都是对上一行“读出来”
比如第2行,是对第1行的描述,意思是:1个1,1个3,所以是:1113
第3行,意思是:3个1,1个3,所以是:3113
请你编写一个程序,可以从初始数字开始,连续进行这样的变换。
输入
第一行输入一个数字组成的串,不超过100位
第二行,一个数字n,表示需要你连续变换多少次,n不超过20
输出
输出一个串,表示最后一次变换完的结果。
样例输入
5
7
样例输出
13211321322115
解题思路:
模拟过程!
代码如下:
#include <iostream>
#include <cstring>
using namespace std;int main() {string a, str;cin >> a;int n;cin >> n;while (n--) {int len = a.length();str = "";for (int i = 0; i < len; i++) {int j = i + 1, cnt = 1;while (a[i] == a[j]) {j++;cnt++;}str += (cnt + '0');str += a[i];i = j - 1;//因为进入循环以后,i会++}a = str;}cout << a << endl;return 0;
}