A1140. Look-and-say Sequence
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>
#include<cstdio>
#include<string>
using namespace std;
int hashTB[] = {};
int main(){
string ss = "";
int D, N;
cin >> D >> N;
ss = ss + (char)('' + D);
for(int i = ; i < N - ; i++){
char pre = ss[];
int cnt = ;
string ss2 = "";
for(int j = ; j < ss.length(); j++){
if(ss[j] == pre){
cnt++;
}else{
ss2 += pre;
ss2 += (char)(cnt + '');
pre = ss[j];
cnt = ;
}
}
ss2 += pre;
ss2 += (char)(cnt + '');
ss = ss2;
}
cout << ss;
cin >> N;
return ;
}
总结:
1、题意:题意不好理解时可以根据标题来猜意思。这题的意思让你看并且读出一个序列, 如一个序列是18882111,则可以读为1个1,3个8,1个2,3个1,再将其写成位在前个数描述在后的序列:11,83,21,13。再对这个新序列进行描述。
A1140. Look-and-say Sequence的更多相关文章
- PAT A1140 Look-and-say Sequence (20 分)——数学题
Look-and-say sequence is a sequence of integers as the following: D, D1, D111, D113, D11231, D112213 ...
- PAT_A1140#Look-and-say Sequence
Source: PAT A1140 Look-and-say Sequence (20 分) Description: Look-and-say sequence is a sequence of i ...
- 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 ...
随机推荐
- C#Note13:如何在C#中调用python
前言 IronPython 是一种在 .NET 及 Mono上的 Python 实现,由微软的 Jim Hugunin(同时也是 Jython 创造者) 所发起,是一个开源的项目,基于微软的 DLR ...
- python之路--递归, 二分法
一 . 递归 自己调用自己, 递归的入口(参数) 和 出口(return), 树形结构的遍历. def func(): print("我是递归") func() func() ...
- python数据结构与算法第七天【链表】
1.链表的定义 如图: 注意: (1)线性表包括顺序表和链表 (2)顺序表是将元素顺序地存放在一块连续的存储区里 (3)链表是将元素存放在通过链构造的存储快中 2. 单向链表的实现 #!/usr/bi ...
- Google浏览器解决编码乱码问题
新版google浏览器编码乱码没有设置的入口,怎么办呢?. 步骤一: 可以下载goole的插件,名为charset,下载后的文件名为Charset_v0.4.1 步骤二: google右上角-> ...
- PHP金额工具类之将阿利伯数字转换为大写中文数字
1.将阿拉伯数字转换为中文大写数字 <?php namespace core\components; class PriceHelper extends \yii\base\Component{ ...
- LOADING Redis is loading the dataset in memory Redis javaAPI实例
今天在实现Redis客户端API操作Jedis的八种调用方式详解中,遇到了LOADING Redis is loading the dataset in memory错误,经过多番查找资料,找到了解决 ...
- MySQL 5.7 关闭严格模式
If your app was written for older versions of MySQL and is not compatible with strict SQL mode in My ...
- Matlab提供了两种除法运算:左除(\)和右除(/)
Matlab提供了两种除法运算:左除(\)和右除(/).一般情况下,x=a\b是方程a*x =b的解,而x=b/a是方程x*a=b的解.例:a=[1 2 3; 4 2 6; 7 4 9]b ...
- Nintex History in Form Table
一.设置参数 二.调用WebService 三.For Each 调用 四.拼写HTML Table 结果: 特别提示:过滤人只要根据人来循环即可
- vuex2.0 基本使用(1) --- state
Vuex 的核心是 store, 它是一个通过 Vuex.Store 构造函数生成的对象.为什么它会是核心呢?因为我们调用这个构造函数创建store 对象的时候,给它传递参数中包装了state, mu ...