PAT甲级——A1140 LookAndSaySequence【20】
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:
1123123111Solution:
能不能读懂这道题是个关键
举例序列:
D D1 D111 D113 D11231
解释:
D 怎么得到 D1
D中有 D 1个 ==》 D1
D1怎么得到D111
D1中有D 1个,1 1个 ==> D1 11 D111
D111怎么得到D113
D111中有D1个,1 3个 ==》 D1 13 D113
记住,D是输入的那个数字!!!!!我就没想通这点,坑死了
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s;
int n;
cin >> s >> n;
for (int cnt = ; cnt < n; ++cnt)
{
string str = "";
int k;
for (int i = ; i < s.length(); i=k)//计算各个字母出现的个数, i=k,从不重复的字母开始
{
for (k = i; k < s.length() && s[k] == s[i]; ++k);//计算相同的字母个数
str += s[i] + to_string(k - i);//将计算的该字母和其相同的次数算入
}
s = str;
}
cout << s;
return ;
}
PAT甲级——A1140 LookAndSaySequence【20】的更多相关文章
- PAT 甲级 1035 Password (20 分)(简单题)
1035 Password (20 分) To prepare for PAT, the judge sometimes has to generate random passwords for ...
- PAT甲级——1035 Password (20分)
To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...
- PAT 甲级 1008 Elevator (20)(代码)
1008 Elevator (20)(20 分) The highest building in our city has only one elevator. A request list is m ...
- PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)
1077 Kuchiguse (20 分) The Japanese language is notorious for its sentence ending particles. Person ...
- PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)
1061 Dating (20 分) Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...
- PAT 甲级 1008 Elevator (20)(20 分)模拟水题
题目翻译: 1008.电梯 在我们的城市里,最高的建筑物里只有一部电梯.有一份由N个正数组成的请求列表.这些数表示电梯将会以规定的顺序在哪些楼层停下.电梯升高一层需要6秒,下降一层需要4秒.每次停下电 ...
- PAT甲级——1061 Dating (20分)
Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...
- PAT甲级——1005.SpellItRight(20分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output e ...
- PAT甲级——1077.Kuchiguse(20分)
The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...
随机推荐
- Asp.Net Core 第06局:中间件
总目录 前言 本文介绍Asp.Net Core 中间件. 环境 1.Visual Studio 2017 2.Asp.Net Core 2.2 开局 第一手:中间件概述 1.中间件:添加到应用 ...
- 面试题:Nginx负载均衡的算法怎么实现的?为什么要做动静分离?
面试题 Nginx负载均衡的算法怎么实现的?Nginx 有哪些负载均衡策略?Nginx为什么要做动静分离? 面试官心理剖析 主要是看应聘人员对Nginx的基本原理是否熟悉,需要应聘人员能够根据实际业务 ...
- Linux下MySQL 命令导入导出sql文件
导出数据库 直接使用命令: mysqldump -u root -p database >database.sql 然后回车输入密码就可以了: mysqldump -u 数据库链接用户名 -p ...
- ss证书问题
#SSL--校验网站证书 #一.什么是SSL证书 from urllib import request #ssl免验证 import ssl ssl._create_default_https_con ...
- dataframe字段过长被截断
总之能,情况就是这样. 看看df类型: 64位明显不够用啊. 网上找到了segmentfault有这个问题,上面说试试 pd.set_option('display.width', 200) ,再百度 ...
- SQL数据库—<2>数据库基本操作(CRUD)
学习基本操作语句之前先简单了解数据库的基本数据类型: 数据类型: 二进制数据:以十六进制形式存储 固定长度:binary 可变长度:varbinary 字符数据 ...
- java 深入剖析ThreadLocal
一.对ThreadLocal中的理解 ThreadLocal的,很多地方叫做线程本地变量,也有些地方叫做线程本地存储,其实意思差不多.可能很多朋友都知道的ThreadLocal为变量在每个线程中都创建 ...
- Tkinter初体验
一.基本步骤 1.导入Tkinter模块 2.创建根窗口 3.填充组件 4.组件关联逻辑 5.进入主循环 二.Code #coding:utf-8 ''' 网关流量校验器 @author: Hongz ...
- H2数据库做单测数据库时踩到的坑
H2数据库用来做单测数据库,可以自定义初始化数据,不用担心数据库内容更改造成单测跑不过问题,不过H2数据库跟实际使用的Mysql还是有一定区别. 1. H2数据库不支持Mysql的批量更新功能,支持批 ...
- 前端学习(二十二)css3(笔记)
html5 普通: header section footer nav article aside figure 特殊: canvas video audio ...