1140 Look-and-say Sequence (20 分)

Look-and-say sequence is a sequence of integers as the following:

D, D1, D111, D113, D11231, D112213111, ...

where D is in [0, 9] except 1. The (n+1)st number is a kind of description of the nth number. For example, the 2nd number means that there is one D in the 1st number, and hence it is D1; the 2nd number consists of one D (corresponding to D1) and one 1 (corresponding to 11), therefore the 3rd number is D111; or since the 4th number is D113, it consists of one D, two 1's, and one 3, so the next number must be D11231. This definition works for D = 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digit D.

Input Specification:

Each input file contains one test case, which gives D (in [0, 9]) and a positive integer N (≤ 40), separated by a space.

Output Specification:

Print in a line the Nth number in a look-and-say sequence of D.

Sample Input:

1 8

Sample Output:

1123123111

题目大意:描述序列,每一个序列都是描述前一个序列的。要求给出第n个序列。

//本来一看差点懵了,但是告诉自己这个我肯定会做,然后就写出来啦,就是简单地找规律而已。

#include <iostream>
#include <algorithm>
#include <vector>
#include<string.h>
#include<string>
#include<cstdio>
using namespace std; string get(int c){//将计数转换为字符串
string s;
while(c!=){
s+=(c%+'');
c/=;
}
reverse(s.begin(),s.end());
return s;
}
int main()
{
string s1,s2;
int n;
cin>>s1>>n;
//s1=s1+"1";
for(int i=;i<n;i++){
int ct=;
for(int j=;j<s1.size();j++){
while(s1[j]==s1[j+]&&j<s1.size()-){
ct++;j++;
}
s2+=s1[j]+get(ct);
ct=;//这里ct要转化为字符串。
}
s1=s2;
s2="";
}
cout<<s1; return ;
}

1.一开始直接用ct+'0'出现了乱码的情况,然后就简单地写了一个函数,转换为字符串,况且对于ct>10的那种也没法通过+‘0’直接转换了

2.后来提交有一个测试点过不去,后来思考发现是因为自己一开始一进来就把s1+“1”,这样是不对的。修改了一下就可以了。

PAT 1140 Look-and-say Sequence [比较]的更多相关文章

  1. PAT 1140 Look-and-say Sequence

    1140 Look-and-say Sequence (20 分)   Look-and-say sequence is a sequence of integers as the following ...

  2. [PAT] 1140 Look-and-say Sequence(20 分)

    1140 Look-and-say Sequence(20 分)Look-and-say sequence is a sequence of integers as the following: D, ...

  3. PAT 解题报告 1051. Pop Sequence (25)

    1051. Pop Sequence (25) Given a stack which can keep M numbers at most. Push N numbers in the order ...

  4. PAT (Advanced Level) 1051. Pop Sequence (25)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  5. PAT (Advanced Level) 1085. Perfect Sequence (25)

    可以用双指针(尺取法),也可以枚举起点,二分终点. #include<cstdio> #include<cstring> #include<cmath> #incl ...

  6. 【PAT甲级】1085 Perfect Sequence (25 分)

    题意: 输入两个正整数N和P(N<=1e5,P<=1e9),接着输入N个正整数.输出一组数的最大个数使得其中最大的数不超过最小的数P倍. trick: 测试点5会爆int,因为P太大了.. ...

  7. 【PAT甲级】1051 Pop Sequence (25 分)(栈的模拟)

    题意: 输入三个正整数M,N,K(<=1000),分别代表栈的容量,序列长度和输入序列的组数.接着输入K组出栈序列,输出是否可能以该序列的顺序出栈.数字1~N按照顺序随机入栈(入栈时机随机,未知 ...

  8. PAT Advanced 1140 Look-and-say Sequence (20 分)

    Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...

  9. PAT甲级——1140.Look-and-say Sequence (20分)

    Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...

随机推荐

  1. libcurl库的编译

    终于弄懂了libcurl库的编译,记下来免得忘记. 下载地址:   libcurl库:http://curl.haxx.se/latest.cgi?curl=zip   openssl安装包:http ...

  2. 超酷的JavaScript叙事性时间轴(Timeline)类库

    在线演示 Timeline 是我见过的最酷的展示事件随时间发展的javascript实现.你可以基于时间使用讲故事的方式来创建时间轴特效,整个时间轴以幻灯的方式来展示,你可以穿插图片,视频或者是网站, ...

  3. 数据库 proc编程五

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <stri ...

  4. 【BZOJ】1622: [Usaco2008 Open]Word Power 名字的能量(dp/-模拟)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1622 这题我搜的题解是dp,我也觉得是dp,但是好像比模拟慢啊!!!! 1400ms不科学! 设f[ ...

  5. redhat7搭建本地yum仓库

    ftp服务器搭建 安装vsftpd yum install vsftpd 配置启动服务和防火墙 修改配置,由于默认不开启主动模式,需要配置开启,在/etc/vsftpd/vsftpd.conf添加如下 ...

  6. Gradle -- buildScript块与allprojects块及根级别的repositories区别

    http://blog.sina.com.cn/s/blog_72ef7bea0102vvg3.html

  7. Asp.net中的Cache--HttpRuntim.Cache 和 HttpContext.Current.Cache

    在ASP.NET中有两个类都提供缓存支持, 一个是HttpRuntime类的Cache属性, 另一个是HttpContext类的Cache属性. 通过查看这两个属性的类型可以发现其实这两个属性都是Sy ...

  8. RESTful作用与特性

    最近在项目中要使用rest风格的设计,学习了一下. 知乎网友说的一句话精确的解释了REST: URL定位资源,用HTTP动词(GET,POST,DELETE,DETC)描述操作-(https://ww ...

  9. codevs 5965 [SDOI2017]新生舞会

    分数规划的裸题. 不会分数规划的OIer.百度:胡伯涛<最小割模型在信息学竞赛中的应用> /* TLE1: last:add(i,j+n,1e9,(real)((real)a[i][j]- ...

  10. Django学习笔记第三篇--关于响应返回

    一.返回简单类型: #1.返回简单字符串 #from django.http import HttpResponse return HttpResponse("return string&q ...