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 ...
随机推荐
- svn没有权限检出项目
解决方法 鼠标右键,svn,setings
- appium常见问题09_MAC打开uiautimatorviewer闪退怎么办?
问题: 下载安装Android SDK后,并且已在.bash_profile文件中配置环境变量.但是在tools中打开定位工具uiautomatorviewer出现闪退. 解决: 首先检查环境变量配置 ...
- JDK1.8 - > 1.7
原文地址 https://blog.csdn.net/hwjean/article/details/52537722 JDK 1.8 -> 1.7 1. 配置好环境变量(我的是64bit系统) ...
- HDU 1028 Ignatius and the Princess III (生成函数/母函数)
题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...
- 转:父类私有变量是否被子类继承详细解说(答案:内存中存在,但sun公司定义为不继承)
应作者要求,本处提供一个连接,表示对原作者版权尊重. https://blog.csdn.net/mr_duantao/article/details/50966471
- QTP加载第三方DLL(C#)实现清除IE缓存(转)
由于QTP的默认编程语言是VBS, 而VBS是一种相对来说功能比较局限的脚本语言,因此我们在编写自动化测试脚本时会有很多功能无法很好的实现. 相对来说c#是一种高级编程语言, 可以实现大多数windo ...
- Ubuntu解压缩rar格式文件
解压缩rar文件时,出现问题 解决方法: sudo apt-get install unrar
- vue - blog开发学习6
1.问题,如下图,使用iviewui中的card导致页面不能出现滚动条(不太会弄,在网上查了一个vue组件vuescroll,因此使用这个做滚动条) 2.安装vuescroll cnpm instal ...
- ubuntu16.04安装LNMP(ubuntu+Nginx+mysql+PHP7.0)
系统环境: Ubuntu 16.04.2 LTS nginx version: nginx/1.10.3 (Ubuntu) PHP 7.0.22-0ubuntu0.16.04.1 mysql Ver ...
- js高级编程思想
js惰性思想: 能够执行一次就搞定绝对不会执行第二次 function createXHR(){ var xhr=null, falg=false, ary=[ function(){ return ...