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. 用css制作星级评分

    Step 1: XHTML <ul class="star-rating">       <li><a href="#" titl ...

  2. int main(int argc, char *argv[])中的argc和argv

    argc 是 argument count的缩写,表示传入main函数的参数个数: argv 是 argument vector的缩写,表示传入main函数的参数序列或指针,并且第一个参数argv[0 ...

  3. An Edge-Guided Image Interpolation Algorithm via Directional Filtering and Data Fusion【翻译】

    基于定向滤波和数据融合的边缘引导图像插值算法 http://ieeexplore.ieee.org/document/1658087/ 摘要: 保留边缘结构对于从低分辨率对应物重建高分辨率图像的图像插 ...

  4. ConfigParser 读写配置文件

    一.ini: 1..ini 文件是Initialization File的缩写,即初始化文件,是windows的系统配置文件所采用的存储格式 2.ini文件创建方法: (1)先建立一个记事本文件.(2 ...

  5. poj_3628 动态规划

    题目大意 有N个数字,大小为a[i], 给定一个数S,用这N个数中的某些数加起来使得结果sum>= S,且sum-S最小,求该最小的sum-S值. 题目分析 题意中可知,这N个数字的和肯定大于S ...

  6. LAMP集群项目四 安装apache、php及其插件

    rpm -qa httpd* 查看是否有apache rpm -e httpd-2.2.22.2  卸载该文件,如果不让卸载,则加参数:--nodeps 不做软件中的依赖检查 ./configure ...

  7. Scrapy使用详细记录

    这几天,又用到了scrapy框架写爬虫,感觉忘得差不多了,虽然保存了书签,但有些东西,还是多写写才好啊 首先,官方而经典的的开发手册那是需要的: https://doc.scrapy.org/en/l ...

  8. LeetCode-Combination Sum IV

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

  9. open live writer 代码着色插件

    插件地址1: http://files.cnblogs.com/files/zsy/OpenLiveWriter.CNBlogs.SourceCode.zip 下载,解压,把里面的文件放到最新的 ap ...

  10. Ubuntu 只能用guest登录的问题修复

    方式1(推荐): 1.先进入guest登入 2.通过Ctrl+Alt+F1进入命令行窗口(注释:Ctrl+Alt+F1~F6进入终端命令行,Ctrl+Alt+F7是退出终端) 3.登录root,然后进 ...