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 ...
随机推荐
- s11 day106-107 RBAC模块
一.登录 把权限存在session中 1. rbac models from django.db import models class Permission(models.Model): " ...
- Java + selenium 元素定位(4)之By CSS
这篇我要介绍元素定位的倒数第二个方法啦,就是基于CSS的元素定位.关于一些CSS的知识,我这里就不累赘的讲了,以后可能会单独写一篇关于CSS的介绍.当然个人推荐如果之前完全没有CSS只是储备的,可以选 ...
- 带有headers的urllib库爬取
#请求头 #1.引入模块 from urllib import request #2.操作 #(1)定义目标url base_url = "http://www.langlang2017.c ...
- __str__和__repr__的区别
有时候我们想让屏幕打印的结果不是对象的内存地址,而是它的值或者其他可以自定义的东西,以便更直观地显示对象内容,可以通过在该对象的类中创建或修改__str__()或__repr__()方法来实现(显示对 ...
- C#break和continue学习
一,代码 static void Main(string[] args) { ; while (true) { int num = Convert.ToInt32(Console.ReadLine() ...
- Ruby 读书
输出: print printf 既定格式输出 puts 自动换行 p 显示对象 sprintf 不规则字符串 pp 需要导入库 putc(字母) 转移字符和单双引号 include Math或者直 ...
- 一、创建且运行JPA工程
1. 创建JPA 工程 (1)选择创建 JPA Project,注意不是Java Project (2)JPA version选择 2.0 (3)选择用户库,否则会出现 At least one us ...
- 力扣—gray code (格雷编码) python实现
题目描述: 中文: 格雷编码是一个二进制数字系统,在该系统中,两个连续的数值仅有一个位数的差异. 给定一个代表编码总位数的非负整数 n,打印其格雷编码序列.格雷编码序列必须以 0 开头. 英文: Th ...
- python-django_rest_framework中的request/Response
rest_framework中的request是被rest_framework再次封装过的,并在原request上添加了许多别的属性: (原Django中的request可用request._requ ...
- 一次CTS引发的网络故障
接到业务部门通知,A机房(库a)到B机房(库b)之间的数据库服务器之间的网络带宽异常突增,影响公司对外业务的整体带宽.一接到通知,作为数据库管理对所涉及的IP还是比较敏感.第一反应就是可能当时主库产生 ...