Source:

PAT A1140 Look-and-say Sequence (20 分)

Description:

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

Keys:

  • 简单模拟

Attention:

  • 这种小题有时候还挺头疼的-,-
  • to_string()

Code:

 /*
Data: 2019-05-24 10:44:34
Problem: PAT_A1140#Look-and-say Sequence
AC: 17:49 题目大意:
观察并说出相应的序列;
比如给出第一个数字D,第二个数字为D1(D有1个)
第三个数字为D111(D有1个,1有1个);
第四个数字为D113(D有1个,1有3个);
以此类推....
输入:
初始数字D,和轮次N
输出:
第N轮相应的序列
*/
#include<cstdio>
#include<string>
#include<iostream>
using namespace std; int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n;
string s;
cin >> s >> n;
for(int k=; k<n; k++)
{
string t="";
for(int i=; i<s.size(); i++)
{
int cnt=;
while(i+<s.size() && s[i]==s[i+])
{
cnt++;
i++;
}
t += (s.substr(i,)+to_string(cnt));
}
s=t;
}
cout << s; return ;
}

最新文章

  1. C# VLCPlayer视频播放器(附源码)
  2. #mysql:command not found
  3. C++二维码相关库编译
  4. .c文件如何编译为ko的MAKEFILE文件编写
  5. AjaxPro 的基本用法
  6. VS2013自动注释插件
  7. 剑指offer系列52---约瑟夫环问题
  8. winform 制作圆形图片框
  9. 查看Linux系统文本编码-方便修改ssh编码一致
  10. c#基础——for循环嵌套经典练习题(打★)
  11. .Net 异步方法, await async 使用
  12. Windows Server 2016-安装AD域服务注意事项
  13. win2003远程桌面怎么切换到多用户?
  14. 将Integer赋值给int(空指针异常)
  15. HLSL
  16. Eclipse设置之:代码注释/server 控制台输出乱码解决
  17. 【校招面试 之 剑指offer】第16题 数值的整数次方
  18. 2018-2019第一学期C语言助教总结
  19. 图论---POJ 3660 floyd 算法(模板题)
  20. 八.安装Nginx

热门文章

  1. [bzoj2989]数列_KD-Tree_旋转坐标系
  2. Spring MVC中&lt;mvc:annotation-driven /&gt;和&lt;context:annotation-config /&gt;的区别分析
  3. 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端。前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架)。
  4. linux下的C语言开发(gdb调试)
  5. oc55--ARC单个对象的内存管理
  6. python 函数参数的传递(参数带星号的说明)
  7. ”W: GPG 错误:http://ppa.launchpad.net lucid Release: 由于没有公钥,无法验证下列签名:“的问题
  8. HTML 打印 换页
  9. POJ 1160 DP
  10. SnackDown Online Qualifier 2017