奇怪的数列
题目描述
从X星截获一份电码,是一些数字,如下:
13
1113
3113
132113
1113122113
…
YY博士经彻夜研究,发现了规律:
第一行的数字随便是什么,以后每一行都是对上一行“读出来”
比如第2行,是对第1行的描述,意思是:1个1,1个3,所以是:1113
第3行,意思是:3个1,1个3,所以是:3113
请你编写一个程序,可以从初始数字开始,连续进行这样的变换。
输入
第一行输入一个数字组成的串,不超过100位
第二行,一个数字n,表示需要你连续变换多少次,n不超过20
输出
输出一个串,表示最后一次变换完的结果。
样例输入 Copy
5
7
样例输出 Copy
13211321322115
思路
方法一
#include <iostream>
using namespace std;
int num[11];
int main() {
long long x,res;
long long index=1, ans,c,a,n,k;
for(int i=0;i<11;i++){
num[i]=0;
}
//13 13%10=3; 13 2
scanf("%ld %d",&x,&n);
for(int i=1;i<=n;i++){
c=0;a=0; k=0; ans=0; index=1;
while(x!=0){
a=0;c=0; k=0;
while(c==a){
a=x%10; //a=3; a=1;
k++; //k=1; k=1;
x=x/10; //x=1; x=0;
c=x%10; //c=1; c=0;
}
ans=(k*10+a)*index+ans;
index=index*100;
cout<<a<<' '<<k<<' '<<x<<' '<<c<<' '<<ans<<' '<<index<<endl;
res=ans;
}
// cout<<ans<<endl;
x=ans;
}
cout<<ans;
return 0;
}
这个不能全过,因为long long无法表示超过20位的数。
方法二
#include<iostream>
#include<iterator>
#include<stdio.h>
#include<iomanip>
#include<string>
#include<cstring>
#include<string.h>
#include<vector>
#include<algorithm>
#include<stdio.h>
#include<math.h>
#include<map>
#include<set>
#include<vector>
#include<stack>
#include<queue>
#include<sstream>
using namespace std;
int main()
{
string s;
cin >> s;
int n;
cin >> n;
while (n--)
{
string temp;
int temp1 = 1;
for (int i = 0; i < s.length()-1; i++){
if (s[i + 1] == s[i]){
temp1++;
}
else{
stringstream ss;
string a;
ss << temp1;
ss >> a;
temp += a;
temp1 = 1;
temp += s[i];
}
}
if (s.length() == 1){
temp = '1';
temp += s[0];
}
else{
stringstream ss;
string a;
ss << temp1;
ss >> a;
temp += a;
temp1 = 1;
temp += s[s.length()-1];
}
s = temp;
}
cout << s;
return 0;
}