PAT_A1140#Look-and-say Sequence
Source:
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 oneD
in the 1st number, and hence it isD1
; the 2nd number consists of oneD
(corresponding toD1
) and one 1 (corresponding to 11), therefore the 3rd number isD111
; or since the 4th number isD113
, it consists of oneD
, two 1's, and one 3, so the next number must beD11231
. This definition works forD
= 1 as well. Now you are supposed to calculate the Nth number in a look-and-say sequence of a given digitD
.
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 ;
}
最新文章
- C# VLCPlayer视频播放器(附源码)
- #mysql:command not found
- C++二维码相关库编译
- .c文件如何编译为ko的MAKEFILE文件编写
- AjaxPro 的基本用法
- VS2013自动注释插件
- 剑指offer系列52---约瑟夫环问题
- winform 制作圆形图片框
- 查看Linux系统文本编码-方便修改ssh编码一致
- c#基础——for循环嵌套经典练习题(打★)
- .Net 异步方法, await async 使用
- Windows Server 2016-安装AD域服务注意事项
- win2003远程桌面怎么切换到多用户?
- 将Integer赋值给int(空指针异常)
- HLSL
- Eclipse设置之:代码注释/server 控制台输出乱码解决
- 【校招面试 之 剑指offer】第16题 数值的整数次方
- 2018-2019第一学期C语言助教总结
- 图论---POJ 3660 floyd 算法(模板题)
- 八.安装Nginx
热门文章
- [bzoj2989]数列_KD-Tree_旋转坐标系
- Spring MVC中<;mvc:annotation-driven />;和<;context:annotation-config />;的区别分析
- 前端控制器是整个MVC框架中最为核心的一块,它主要用来拦截符合要求的外部请求,并把请求分发到不同的控制器去处理,根据控制器处理后的结果,生成相应的响应发送到客户端。前端控制器既可以使用Filter实现(Struts2采用这种方式),也可以使用Servlet来实现(spring MVC框架)。
- linux下的C语言开发(gdb调试)
- oc55--ARC单个对象的内存管理
- python 函数参数的传递(参数带星号的说明)
- ”W: GPG 错误:http://ppa.launchpad.net lucid Release: 由于没有公钥,无法验证下列签名:“的问题
- HTML 打印 换页
- POJ 1160 DP
- SnackDown Online Qualifier 2017