Source:

PAT A1140 Look-and-say Sequence (20 分)

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 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

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的更多相关文章

  1. oracle SEQUENCE 创建, 修改,删除

    oracle创建序列化: CREATE SEQUENCE seq_itv_collection            INCREMENT BY 1  -- 每次加几个              STA ...

  2. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

  3. DG gap sequence修复一例

    环境:Oracle 11.2.0.4 DG 故障现象: 客户在备库告警日志中发现GAP sequence提示信息: Mon Nov 21 09:53:29 2016 Media Recovery Wa ...

  4. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Sequence Reconstruction 序列重建

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

  6. [LeetCode] Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  7. [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 ...

  8. [LeetCode] Longest Consecutive Sequence 求最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

随机推荐

  1. UVA 10859 树形DP

    很明显的树形DP了,设状态dp[i][0],dp[i][1].枚举子节点放或不放的两种状态. 在此学到一种不同于一般处理的方法,题目要求被两灯照亮的边尽量多,反过来即被一灯照亮的尽量少设为e.又需要的 ...

  2. Navicat 提示Cannot create oci environment 解决方式

    一直在使用Navicat,这是一个数据库client软件.能连接多种不同类型的数据库,给我们的日常的工作带来了不少的便捷.近期.我在电脑上安装了orcale,然后,Navicat就莫名其妙的不能连接o ...

  3. 2014年辛星jquery解读第三节 Ajax

    ***************Ajax********************* 1.Ajax是Asynchronous Javascript And  XML的简写,它指的是异步Javascript ...

  4. Chrome development tools学习笔记(3)

    (上次DOM的部分做了些补充,欢迎查看Chrome development tools学习笔记(2)) 利用DevTools Elements工具来调试页面样式 CSS(Cascading Style ...

  5. java调用c++ dll出现中文乱码

    近期的开发用到了使用java调用本机动态连接库的功能,将文件路径通过java调用C++代码对文件进行操作. 在调用中假设路径中包括有中文字符就会出现故障.程序执行就会中止. 以下用一个小样例,来说明记 ...

  6. HDU5489 LIS变形

    Removed Interval Problem Description Given a sequence of numbers A=a1,a2,…,aN , a subsequence b1,b2, ...

  7. Get-Acl 查看文件权限

    https://blogs.msmvps.com/erikr/2007/09/26/set-permissions-on-a-specific-service-windows/ Get-Acl .\L ...

  8. bzoj3110 [Zjoi2013]K大数查询——线段树套线段树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3110 外层权值线段树套内层区间线段树: 之所以外层权值内层区间,是因为区间线段树需要标记下传 ...

  9. E20170915-hm

    client n. 顾客; 当事人; 诉讼委托人; [计算机] 客户端; seal  n. 密封; 印章; 海豹; 封条;  v. 密封; 盖章; 决定; 封上(信封); sheet  n. 纸; 被 ...

  10. ArrayList 扩容原理

    面试中经常问到的问题之一就是List的扩容机制了,他是怎么做到扩容的,大家都能答出来底层是数组,复制一个数组来扩容,但是再具体一点来说,大家就不知道该怎么说了,如果不看源码说这么多确实就差不多了,但是 ...