蓝桥杯题目练习 提升篇 [蓝桥杯2015决赛]奇怪的数列

本文介绍了一种奇特的数列生成算法,通过解析X星电码中的数字模式,运用编程技巧实现数列的连续变换。从任意初始数字开始,程序能够准确地生成后续的数列,并详细展示了两种实现方法,包括直接数值运算和字符串处理,适用于解决类似数学谜题。

奇怪的数列

题目描述

从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;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值