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

  1. PAT 甲级 1035 Password (20 分)(简单题)

    1035 Password (20 分)   To prepare for PAT, the judge sometimes has to generate random passwords for ...

  2. PAT甲级——1035 Password (20分)

    To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem ...

  3. PAT 甲级 1008 Elevator (20)(代码)

    1008 Elevator (20)(20 分) The highest building in our city has only one elevator. A request list is m ...

  4. PAT 甲级 1077 Kuchiguse (20 分)(简单,找最大相同后缀)

    1077 Kuchiguse (20 分)   The Japanese language is notorious for its sentence ending particles. Person ...

  5. PAT 甲级 1061 Dating (20 分)(位置也要相同,题目看不懂)

    1061 Dating (20 分)   Sherlock Holmes received a note with some strange strings: Let's date! 3485djDk ...

  6. PAT 甲级 1008 Elevator (20)(20 分)模拟水题

    题目翻译: 1008.电梯 在我们的城市里,最高的建筑物里只有一部电梯.有一份由N个正数组成的请求列表.这些数表示电梯将会以规定的顺序在哪些楼层停下.电梯升高一层需要6秒,下降一层需要4秒.每次停下电 ...

  7. PAT甲级——1061 Dating (20分)

    Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...

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

  9. PAT甲级——1077.Kuchiguse(20分)

    The Japanese language is notorious for its sentence ending particles. Personal preference of such pa ...

随机推荐

  1. Makefile之自动化变量

    makefile自动化变量在大型项目的Makefile使用的太普遍了,如果你看不懂自动化变量,开源项目的makefile你是看不下去的. 以往总是看到一些项目的makefile,总是要翻gnu的Mak ...

  2. Android深度探索-卷1第二章心得体会

    这章介绍了搭建Android开发环境的的搭建,主要是在Linux上搭建Android开发环境总体来说因为都是在Linux下开发的,so,只介绍了在Linux环境下的搭建在搭建过程中全是命令操作,和Wi ...

  3. 在WSL Ubuntu 下编译UPX详细步骤

    准备环境: 1. sudo apt-get update 2. sudo apt-get clang 3. apt-get install libstdc++-dev Reading package ...

  4. EasyUI 的日期控件单击文本框显示日历

    注意:可 用 ctrl+f 搜索 "_outerWidth():0" 1. jQuery.easyui.min.js1.3.2 版本   function _745(_746,_7 ...

  5. Linux服务器攻击防御(转)

    攻击小贴士 如果你打算做一些搓事,那么你需要采取一些措施来保护自己.这是一切的前提,不然我会追杀你,并破坏你的生活. 我总结了几大忌(fuckups): 不要在honeypots执行操作.(在一切开始 ...

  6. ZOJ 3681E - Cup 2(记忆化dfs)不好读

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/opm777/article/details/25726221 E - Cup 2 Time Limi ...

  7. js转换成字符串

    有两种方法: 1.对于boolean, number, string类型,可调用toString()方法 2.用String(var)方法 其中,第二种方法使用范围更广,可将没有toString()方 ...

  8. 关于 ioctl 函数

    ioctl函数是用于控制的设备的接口 1.底层: long (*unlocked_ioctl) (struct file *filp, unsigned int cmd, unsigned long ...

  9. centos系统jdk安装

    下载Oracle官网的jdk来安装 不使用openjdk 最新的官网地址: https://www.oracle.com/technetwork/java/javase/downloads/jdk8- ...

  10. XML 和 HTML 之间的差异

    XML 和 HTML 为不同的目的而设计: XML 被设计用来传输和存储数据,其焦点是数据的内容. HTML 被设计用来显示数据,其焦点是数据的外观. HTML 旨在显示信息,而 XML 旨在存储和传 ...