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 ...
随机推荐
- JUnit测试提示Java.lang.Exception: No runnable methods
网上一大堆都说,没写@Test,或者是,导包错误,= =然而我并没有发现我有这个毛病 = =最后终于找到罪魁祸首 Junit版本太低!!! Junit版本太低!!! Junit版本太低!!! = =因 ...
- PhpStorm本地断点调试
一.断点调试php环境搭建 1.检测本地php环境是否安装了Xdebug 在本地输出phpinfo():搜索Xdebug;如下图 如果没有安装,安装操作Xdebug如下: 将phpinfo();的信 ...
- Redis的java客户端jedis
导包:Jedis需要的jar包 >Commons-pool-1.6.jar >Jedis-2.1.0.jar 配置:linux防火墙设置,不会设置就关闭. 停止防火墙 systemctl ...
- win10远程桌面连接提示身份验证错误,要求的函数不受支持的解决方案
转自https://www.baidu.com/link?url=67JXh4h79mN47mEenuH_ElGkSh9_GdOiY-Xp9Ihw0_mQIZHrPx-HxY3EIm_nTZKPoRZ ...
- KVM+QEMU虚拟化概念
概念: KVM,即Kernel-basedvirtual machine,由redhat开发,是一种开源.免费的虚拟化技术.对企业来说,是一种可选的虚拟化解决方案. 定义:基于Linux内核的虚拟机 ...
- WEB相关概念、Tomcat初识、Servlet、基本知识。
/* * 一.web的概念? * 1.web就是在http协议基础之上, 利用浏览器进行访问的网站. * Web Page指网站内的网页. 我们常说的WWW(World Wide Web 万维网)就是 ...
- powerdesigner 16.5 不允许有扩展属性,或对象不存在
创建完之后这边会出现 选择刚创建的用户 这样就可以了
- WinForm中在非UI线程更改控件值的办法
从非UI线程调用UI控件赋值.或进行其他更新UI的操作的话,会出现异常: System.InvalidOperationException:“线程间操作无效: 从不是创建控件“xxx”的线程访问它.” ...
- How to create DMG on macOS
hdiutil create -srcfolder /users/test1/ -volname test1 /users/test/test1.dmg
- MySQL各版本解释和下载
MySQL 的官网下载地址:http://www.mysql.com/downloads/ 个人理解: 1.不要再纠结是否是5.1还是5.5.5.6.5.7这些,一般选择时不要选择太新,选择5.1或者 ...