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
#include<iostream>
using namespace std; int main(){
string s;
int n,j;
cin >> s;
cin >> n;
while(--n){
string ans;
int cnt = ;
char pre = s[];
for(int i = ; i < s.size(); i++){
if(s[i] == pre) cnt++;
else{
ans += pre;
ans += cnt + '';
pre = s[i];
cnt = ;
}
}
if(cnt > ){
ans += pre;
ans += cnt +'';
}
s = ans;
}
cout << s;
return ;
}
1140 Look-and-say Sequence (20 分)的更多相关文章
- 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 ...
- 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 ...
- [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, ...
- 1116 Come on! Let's C (20 分)
1116 Come on! Let's C (20 分) "Let's C" is a popular and fun programming contest hosted by ...
- pat 1116 Come on! Let's C(20 分)
1116 Come on! Let's C(20 分) "Let's C" is a popular and fun programming contest hosted by t ...
- PAT甲级:1136 A Delayed Palindrome (20分)
PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...
- 抛弃EF,20分构建一个属于自己的ORM框架
Poiuyt_cyc 博客园首页新随笔联系订阅管理随笔 - 11 文章 - 0 评论 - 111 抛弃EF,20分构建一个属于自己的ORM框架 相信EF大家都不陌生了,因为数据库表跟程序实体是一一 ...
- PTA 邻接表存储图的广度优先遍历(20 分)
6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...
- #020PAT 没整明白的题L1-009 N个数求和 (20 分)
后面的测试点过不去,两个错误一个超时. 目前未解决 L1-009 N个数求和 (20 分) 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和 ...
- L1-023 输出GPLT (20 分)
L1-023 输出GPLT (20 分) 给定一个长度不超过10000的.仅由英文字母构成的字符串.请将字符重新调整顺序,按GPLTGPLT....这样的顺序输出,并忽略其它字符.当然,四种字符(不区 ...
随机推荐
- ubunt 14.04 Could not find CMAKE_ROOT !!! CMake has most likely not been installed correctly. Modul
CMake Error: Could not find CMAKE_ROOT !!! CMake has most likely not been installed correctly. Modul ...
- pip镜像源配置
#配置文件路径:%HOME%\pip\pip.ini,不存在新建即可 #在 Windows 2000 以上版本里,%HOME% 目录指的是系统盘下的“\Documents and Settings\你 ...
- codefirst 最新策略
http://www.yunjuu.com/info/76058.html 在原有数据库中使用 CodeFirst ,除了第一次添加实体后要立即执行一次 Enable-Migrations add-m ...
- 编写高质量代码改善C#程序的157个建议——建议12: 重写Equals时也要重写GetHashCode
建议12: 重写Equals时也要重写GetHashCode 除非考虑到自定义类型会被用作基于散列的集合的键值:否则,不建议重写Equals方法,因为这会带来一系列的问题. 如果编译上一个建议中的Pe ...
- 使用web3部署一个比较复杂的智能合约
以太坊系列之二十一 使用web3部署比较复杂的智能合约 搭建私链上的雷电网络 以太坊系列之二十一 使用web3部署比较复杂的智能合约 1 雷电网络智能合约简单介绍 2 remix 无法部署使用了lib ...
- Java高质量代码之 — 泛型与反射
在Java5后推出了泛型,使我们在编译期间操作集合或类时更加的安全,更方便代码的阅读,而让身为编译性语言的Java提供动态性的反射技术,更是在框架开发中大行其道,从而让Java活起来,下面看一下在使用 ...
- Linux服务器其中一个磁盘满了怎么办?在不做磁盘扩容的情况下,一个软连接就搞定。
适用环境要求:Linux系统及服务器.有管理员权限.存在多余空间的磁盘例如下图中"/home"在磁盘sda5中与"/"不属于同一块磁盘: 1.首先转移正在使用的 ...
- 20165219 《Java程序设计》实验一(Java开发环境的熟悉)实验报告
20165219 <Java程序设计>实验一(Java开发环境的熟悉)实验报告 一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:王彦博 学号:20165219 成绩: 指 ...
- Codeforces Round #551 (Div. 2)B. Serval and Toy Bricks
B. Serval and Toy Bricks time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 金庸笔下的"程序员" | 附金庸武侠全集
金庸 飞雪连天射白鹿,笑书神侠倚碧鸳当您八十高龄取得牛津大学唐朝史学博士学位,我还以为这是另一部史诗开始的信号,然而没有后续了.我的高中到大学,是十遍<笑傲江湖>的距离,我的整个青春,是大 ...