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 ...
随机推荐
- K3 WISE安全认证方式
k/3中间层注册三种安全认证方式: 交互式用户方式,网络服务方式,信任方式,是指组件服务中生成的COM+应用程序中的组件包的运行账户(注册中间层后产生很多ebo开头的和kdsvrmgr组件包). 三种 ...
- shell expr用法
expr 计算整数变量值 使用方法如下: linux-zpycfm:/home/test/shell # s=+ -bash: +: command not found linux-zpycfm:/h ...
- python学习笔记(5-1)-基本数据类型-字符串类型及操作
五.字符串处理函数 len(x):字符串x的长度.如len("12345")结果为5 str(x):任意类型x所对应的字符串形式. >>> str(123) ...
- QTP 自动化测试桌面程序--笔记(关闭 启动程序脚本) 、安装
0 安装qtp .exe 文件 安装 插件文件(如delph) 1 关闭 启动程序: 将要操作的程序-存入localdatatable中 设置 迭代一次 rem SystemUtil.ClosePro ...
- java 中的打印流
package cn.zhou; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.F ...
- Log4j2配置与使用
依赖包: <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <depend ...
- thinkphp视图中插入php代码
性别: <?php if($item['sex'] == 1):?> 男 <?php else:?> 女 <?php endif;?> 错误:<?php ec ...
- Coalesce (MS SQL Server)——取指定内容(列)中第一个不为空的值
oalesce 获得参数中第一个不为空的表达式. 语法: COALESCE ( expression [ ,...n ] ) 例子:CREATE TABLE wages ...
- [开源 .NET 跨平台 Crawler 数据采集 爬虫框架: DotnetSpider] [一] 初衷与架构设计
[DotnetSpider 系列目录] 一.初衷与架构设计 二.基本使用 三.配置式爬虫 四.JSON数据解析与配置系统 五.如何做全站采集 为什么要造轮子 同学们可以去各大招聘网站查看一下爬虫工程师 ...
- codeforces347B
Fixed Points CodeForces - 347B A permutation of length n is an integer sequence such that each integ ...