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. C++ 指针引用

    //指针引用 #include<iostream> using namespace std; struct Teacher{ ]; int age; }; int InitA(Teache ...

  2. 第二百五十六节,Web框架

    Web框架 Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. 举例: #!/usr/bin/env python #c ...

  3. gitolite 服务器搭建

    网上大牛都说的是安装gitolite要建git用户,我看完全没必要,毕竟用户拿到私钥也未必能登录服务器吧...下面我们就用root用户来安装gitolite,并且配置版本库文件保存路径.. 1. gi ...

  4. mysql根据查询结果,创建表

    create table copy_materials_details (SELECT * FROM `materials_details`);

  5. asp.net 动态添加多个用户控件

    动态添加多个相同用户控件,并使每个用户控件获取不同的内容. 用户控件代码: 代码WebControls using System; using System.Collections.Generic;  ...

  6. Sqlite - constraint failed[0x1555]: UNIQUE constraint failed

    执行插入操作时,出现异常constraint failed[0x1555]: UNIQUE constraint failed 意思是:sqlite 唯一约束失败 定位于某个表字段上,该字段是表的主键 ...

  7. winform 递归循环阻止机构

    private void GetTree() { DataTable dt = new DataTable(); var sql = @" select OUID,ParentOUID,OU ...

  8. 使用vue来做局部刷新

    我们都知道,vue的组件化是他最强大的核心所在,路由也是特别可爱的一部分,但是路由适合一些大型的组件,看url路径的时候会出现变化,但是有时候我们想要一些小的局部小刷新,这个时候就需要用到它的动态组件 ...

  9. 【BZOJ4443】[Scoi2015]小凸玩矩阵 二分+二分图最大匹配

    [BZOJ4443][Scoi2015]小凸玩矩阵 Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个数字不能在同一行或 ...

  10. 【BZOJ3781、2038】莫队算法2水题

    [BZOJ3781]小B的询问 题意:有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数 ...