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 ...
随机推荐
- CSS3:box-sizing:不再为盒子模型而烦恼
题外话: W3C奉行的标准,就是content-box,就是须要计算边框,填充还有内容的;可是就我个人而言, 比較喜欢的是传统IE6时候的怪异模式,不用考虑容器是否会被撑开(打乱布局); 盒子模型差异 ...
- hibernate初步2
Hibernate级联设计 数据库表之间的关系(主要关系有一对多.一对一.多对多)主要是从如下三个方面体现出来: 1.表体设计 2.实体类的设计 3.配置文件 以下是一些重要级联设计参数属性介绍: c ...
- python chunk 方式读取大文件——本质上还是file read自身支持
参考:https://stackoverflow.com/questions/519633/lazy-method-for-reading-big-file-in-python 最优雅方式: file ...
- 历届试题 邮局(dfs+剪枝)
历届试题 邮局 时间限制:1.0s 内存限制:256.0MB 问题描述 C村住着n户村民,由于交通闭塞,C村的村民只能通过信件与外界交流.为了方便村民们发信,C村打算在C村建设k ...
- B1045 糖果传递 数学
糖果传递,一开始就想到了n^2的模拟贪心算法,但是一看,数据范围太大,好像只有O(N)能过...没啥方法,只好看题解,之后发现,woc,还有这种操作? 这个题直接可以用数学证明... 证明如下: 首先 ...
- Genesis 多边形闭轮廓填充算法
通过逐行扫描,计算得出直线与多边形相交点进行求解 原理图形如下所示: 相关函数: /// <summary> /// 求点P到线段L距离 /// </summary> /// ...
- 棋盘问题(dfs)
http://poj.org/problem?id=1321 思路:按行搜索,回溯时还原棋盘. #include <stdio.h> #include <string.h> ] ...
- yii的criteria的用法
Yii的Active Recorder包装了很多. 特别是把SQL中 把where,order,limit,IN/not IN,like等常用短句都包含进CDbCriteria这个类中去,这样整个代码 ...
- go之数据类型转换和类型断言
一.类型转换 1.1 简单类型转换 格式 valueOfTypeB = typeB(valueOfTypeA) int 转 float64 package main import "fmt& ...
- 前端常见面试题总结part2
今天总结了几道,感觉非常有意思的题,有感兴趣的可以看下,有疑问请留言~ (答案在最后) 考察自执行函数的this指向 审题要细心 var n = 2, obj = { n:2, fn:(functio ...