问题 1833: [蓝桥杯][2015年第六届真题]奇怪的数列

问题 1833: [蓝桥杯][2015年第六届真题]奇怪的数列

时间限制: 1Sec 内存限制: 128MB 提交: 146 解决: 88

题目描述

从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<algorithm>
using  namespace std;
string change( string s ){
       string t;
	   int l = 0 , r=0 , cnt = 0;
	   for( l=0 ;l < s.length() ;l=r){
	        cnt = 0;
			for( r=l; r<s.length();r++ ){
				 if(  s[l] == s[r] )
				      cnt++;
				 else break;
			} 
		    t = t + to_string(cnt) + s[l]  ;
	   } 
	   return t;
}
int   main() {
      string s;
      cin>>s;
      int n;
      cin>>n;
      while( n-- ){
      	     s = change( s );
	  }
	  cout<<s<<endl;
	  return 0;
}
发布了761 篇原创文章 · 获赞 134 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/S_999999/article/details/104101000