PAT_A1140#Look-and-say Sequence
Source:
Description:
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 oneD
in the 1st number, and hence it isD1
; the 2nd number consists of oneD
(corresponding toD1
) and one 1 (corresponding to 11), therefore the 3rd number isD111
; or since the 4th number isD113
, it consists of oneD
, two 1's, and one 3, so the next number must beD11231
. This definition works forD
= 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digitD
.
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
Keys:
- 简单模拟
Attention:
- 这种小题有时候还挺头疼的-,-
- to_string()
Code:
/*
Data: 2019-05-24 10:44:34
Problem: PAT_A1140#Look-and-say Sequence
AC: 17:49 题目大意:
观察并说出相应的序列;
比如给出第一个数字D,第二个数字为D1(D有1个)
第三个数字为D111(D有1个,1有1个);
第四个数字为D113(D有1个,1有3个);
以此类推....
输入:
初始数字D,和轮次N
输出:
第N轮相应的序列
*/
#include<cstdio>
#include<string>
#include<iostream>
using namespace std; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n;
string s;
cin >> s >> n;
for(int k=; k<n; k++)
{
string t="";
for(int i=; i<s.size(); i++)
{
int cnt=;
while(i+<s.size() && s[i]==s[i+])
{
cnt++;
i++;
}
t += (s.substr(i,)+to_string(cnt));
}
s=t;
}
cout << s; return ;
}
PAT_A1140#Look-and-say Sequence的更多相关文章
- oracle SEQUENCE 创建, 修改,删除
oracle创建序列化: CREATE SEQUENCE seq_itv_collection INCREMENT BY 1 -- 每次加几个 STA ...
- Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等
功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...
- DG gap sequence修复一例
环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Sequence Reconstruction 序列重建
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
- [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Longest Consecutive Sequence 求最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- 洛谷——P1106 删数问题
https://www.luogu.org/problem/show?pid=1106 题目描述 键盘输入一个高精度的正整数N,去掉其中任意k个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对 ...
- war包结构
一个war包里面必含的两个目录是meta-inf和web-inf文件夹 一个war包里面必含的两个目录是meta-inf和web-inf文件夹 一个war包里面必含的两个目录是meta-inf和web ...
- [MongoDB]安装 MongoDB 数据库
1.tar zxvf mongodb.tgz 2.vim ~/.bashrc 增加 MONGODB_HOME 并添加bin到PATH ,source ~/.bashrc 3.mongod -vers ...
- java 执行可执行文件时提示“could not find or load main class ”的问题
- Xsolla与蜗牛一起共创黑金
Xsolla和蜗牛游戏强强合作,公布了黑金在线,是中国知名网络游戏武术时代的一个新项目. Xsolla与蜗牛黑金 2014年6月10日至20日,蜗牛的黑金在线首次在美国洛杉矶E3展会上亮相. 该游戏官 ...
- ORACLE错误1033出现和ORA-00600错误解决的方法
非法关机以后.Oracle数据常常出现这个错误: EXP-00056:ORACLE错误1033出现 ORA-01033:ORACLE initialization or shutdown in pro ...
- 解决Android 应用方法数不能超过65K的问题
Conversion to Dalvik format failed:Unable toexecute dex: method ID not in [0, 0xffff]: 65536 假设你的应用出 ...
- 2017 Multi-University Training Contest - Team 1 1002&&hdu 6034
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- CSS--浏览器CSS Hack 收集
所谓的Hack就是只有特定浏览器才能识别这段hack代码.Hack 不是什么好东西,除非没有办法,我们尽量还是不要用着玩意. 下面是各个浏览器的CSS Hack 列表. Firefox 浏览器 @-m ...
- PCB MS SQL SERVER版本管控工具source_safe_for_sql_server
PCB由于业务关系复杂,业务触发一个事件时,可能需与数据库多个表进行关连处理才能拿到数据结果, 而表关连并不是简单的关连,实际是要进行大量数据筛选,逻辑判断,转换等过程...这个过程是复杂的 想一想, ...