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. 终极方法,pjsip发起多方对讲出错Too many objects of the specified type (PJ_ETOOMANY)

    http://blog.csdn.net/zhangjm_123/article/details/26727221 —————————————————————————————————————————— ...

  2. [android] AndroidManifest.xml【 manifest -> uses-permission】

    在  API Level 1 时被引入 简介: 在某些情况下,你为app设置的权限将会影响到google应用商店会用何种规则来过滤你的APP. 如果你需要一个硬件相关的权限——CAMERA,googl ...

  3. python 2.7安装某些包出现错误:"libxml/xmlversion.h:没有那个文件或目录"

    解决办法: 1. ubuntu系统: 首先: apt-get install libxml2-dev sudo ln -s /usr/include/libxml2/libxml /usr/inclu ...

  4. UDP也需要现有Server端,然后再有Client端

    UDP编程: DatagramSocket(邮递员):对应数据报的Socket概念,不需要创建两个socket,不可使用输入输出流. DatagramPacket(信件):数据包,是UDP下进行传输数 ...

  5. 【BZOJ】1650: [Usaco2006 Dec]River Hopscotch 跳石子(二分+贪心)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1650 看到数据和最小最大时一眼就是二分... 但是仔细想想好像判断时不能贪心? 然后看题解还真是贪心 ...

  6. PHPmailer发送邮件时的常见问题及解决办法

    来源:http://www.chinastor.com/a/jishu/mailserver/0G392262014.html 使用PHPmailer发送邮件时的常见问题总结: 一,没有定义发送邮箱$ ...

  7. AWS系列-使用Could Events定时对EC2打快照

    第1章 使用Could Events定时对EC2打快照 1.1 打开控制台搜索CloudWatch 在搜索栏输入CloudWatch,点击进入CloudWatch控制台 1.2 选择进入Events ...

  8. DecimalFormat 四舍五入Float类型的坑

    今天又踩了一个坑,使用DecimalFormat来完毕四舍五入.可是传入的是float类型,几轮測试才发现一个问题,传入的float会被转为double类型.大家都知道float是4位,double是 ...

  9. 编程之美 set 8 区间重合判断

    Leetcode 上有连续的两道题, 一个是 insert interval, 一个是 merge interval, 其中 insert interval 是 merge interval. 其中 ...

  10. open() 函数以 a+ 模式打开文件

    这种模式打开文件,可读可写,从文件顶部读取内容,从文件底部追加内容,文件不存在则自动创建 [root@localhost ~]$ cat 1.txt aaa bbb ccc In [1]: data ...