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:
1123123111
Solution:
能不能读懂这道题是个关键
举例序列:
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 ...
随机推荐
- Python3学习(18)--偏函数(Partial) --转载存地址
由于最近出差,没有时间更, 倒不是忙,而是费心,项目其实并不难,主要是涉及钱的地方谈技术略显苍白:没有技术解决不了的问题,但是钱没到位,没人愿意无偿给你提供技术,算是停更一周后的,吐槽吧. 赶上今天 ...
- Html5 学习笔记 【PC固定布局】 实战7 机票预订页面
最终实际效果: HTML代码: <!DOCTYPE html> <html lang="zh-cn"> <head> <meta char ...
- 牛客 在其他数都出现k次的数组中找到出现1次的数
题目链接:https://www.nowcoder.com/practice/26e46f1f5e0d48c4b9ba13fe3e8d0ec6?tpId=101&tqId=33216& ...
- Python 2 将死,你准备好了吗?
Python 软件基金会宣布,到 2020 年元旦,将不再为编程语言 Python 2.x 分支提供任何支持.这一天将标志着一出延续多年的戏剧的高潮:Python 从较旧的.功能较弱的.广泛使用的版本 ...
- list中的所有值转换为字符串,以及list拼接成一个字符串
import stringlis=[1,2,3,'abc']fw=open('hello.txt','w',encoding='utf-8')# print(''.join(str(lis).repl ...
- undefined,null,var 0 = {},var s = '',的区别
undefined:不清楚变量的类型:var m; null:知道该变量是对象的引用,但是地址为空 var o = {};这是一个对象,有指向地址,但是值为空 var 0 = '';这是一个空的字符串
- Regular Expression 範例
Regular expression 被實作於各種語言中,可以用來對字串做 比對 擷取 分隔 這幾類的處理.以下是 JavaScript的處理範例. 各位看官,可以按F12開啟 brower 的 de ...
- 2019 牛客多校第一场 B Integration
题目链接:https://ac.nowcoder.com/acm/contest/881/B 题目大意 给定 n 个不同的正整数 ai,求$\frac{1}{\pi}\int_{0}^{\infty} ...
- c# HttpListener 使用
与 IIS 上发布网站相比,使用 HttpListener 编程的程序更加轻量化,易于发布和更新.配合 Thread 或 Task 类也可满足一定的并发. https://docs.microsoft ...
- Excel_PowerQuery——秒杀Vlookup的表合并
终于,Power Query的第二弹来了,距离上一次PQ更博,已经将近半年. Excel_PoweQuery——条件计数.条件求和 使用PQ进行表格数据的连接合并是一件畅快的事情. 下面的数据是我随机 ...