PAT A1140 Look-and-say Sequence (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
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#include <set>
using namespace std;
const int maxn=;
int n,m,k;
int seq[][maxn];
int cnt;
int main(){
scanf("%d %d",&n,&m);
seq[][]=n;
if(m==)printf("%d",n);
cnt=;
for(int i=;i<m;i++){
k=;
int now=seq[i-][],num=;
for(int j=;j<cnt;j++){
if(seq[i-][j]==now){
num++;
}
else{
seq[i][k]=now;
seq[i][k+]=num;
now=seq[i-][j];
num=;
k+=;
}
}
seq[i][k]=now;
seq[i][k+]=num;
k+=;
cnt=k;
}
for(int j=;j<k;j++){
printf("%d",seq[m-][j]);
}
}
注意点:题目看了半天没懂,后来看懂了点理解的是前一个序列有几个什么然后输出,然后第六个样例怎么看都不对。看了大佬的思路,原来是有几个连续的数字,然后输出来。那既然n最大就40,设个二维数组直接枚举就好了,maxn一开始只设了10010,发现最后一个测试点错了,最后一个测试点应该是n=40,结果很大,maxn为1e5就够了
PAT A1140 Look-and-say Sequence (20 分)——数学题的更多相关文章
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642
PAT (Advanced Level) Practice 1008 Elevator (20 分) 凌宸1642 题目描述: The highest building in our city has ...
- PAT甲级:1152 Google Recruitment (20分)
PAT甲级:1152 Google Recruitment (20分) 题干 In July 2004, Google posted on a giant billboard along Highwa ...
- PAT乙级:1072开学寄语(20分)
PAT乙级:1072开学寄语(20分) 题干 下图是上海某校的新学期开学寄语:天将降大任于斯人也,必先删其微博,卸其 QQ,封其电脑,夺其手机,收其 ipad,断其 wifi,使其百无聊赖,然后,净面 ...
- PAT乙级:1057 数零壹 (20分)
PAT乙级:1057 数零壹 (20分) 题干 给定一串长度不超过 105 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一 ...
- PAT乙级:1082 射击比赛 (20分)
PAT乙级:1082 射击比赛 (20分) 题干 本题目给出的射击比赛的规则非常简单,谁打的弹洞距离靶心最近,谁就是冠军:谁差得最远,谁就是菜鸟.本题给出一系列弹洞的平面坐标(x,y),请你编写程序找 ...
- PAT乙级:1084 外观数列 (20分)
PAT乙级:1084 外观数列 (20分) 题干 外观数列是指具有以下特点的整数序列: d, d1, d111, d113, d11231, d112213111, ... 它从不等于 1 的数字 d ...
- PAT乙级:1063 计算谱半径 (20分)
PAT乙级:1063 计算谱半径 (20分) 题干 在数学中,矩阵的"谱半径"是指其特征值的模集合的上确界.换言之,对于给定的 n 个复数空间的特征值 { a1+b1i,⋯,a** ...
- PAT 乙级 1044 火星数字 (20 分)
1044 火星数字 (20 分) 火星人是以 13 进制计数的: 地球人的 0 被火星人称为 tret. 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, j ...
- PAT 甲级 1041 Be Unique (20 分)
1041 Be Unique (20 分) Being unique is so important to people on Mars that even their lottery is desi ...
随机推荐
- failed to install tomcat6 service ,check your setting and permissions
出现的问题: 刚刚重新装了tomcat7,出现提示:Tomcat安装不成功.提示是:failed to install tomcat6 service ,check your setting and ...
- Java基础——Ajax(一)
学习之前举一个简单的小栗子,让我们简单了解一下Ajax的应用与好处.一般网站都会有自己的数据库,用来单独存储用户的个人信息,平时我们在注册账号的时候会遇到信息输入的页面,假设第一个输入信息是用户名的填 ...
- echarts2.0仪表盘
option = { backgroundColor: '#0e0b2a', tooltip : { formatter: "{a} <br/>{b} : {c}%" ...
- php服务器代理解决浏览器跨越问题
详见代码 <?php $url = $_SERVER["QUERY_STRING"]; switch ($_SERVER['REQUEST_METHOD']) { case ...
- LoadRunner11录制脚本出现的问题
问题一:无法启动IE浏览器 原因:设置录制程序的录制填写错误,因为IE有两个一个是32位的一个是64位的 我们需要设置浏览器为IE 32位即可正常运行 问题二:无法录制百度等官网页面 原因:录制选项中 ...
- bs4爬虫入门
# -*- coding: utf-8 -*- """ Created on Fri Nov 16 13:35:33 2018 @author: zhen "& ...
- 洗礼灵魂,修炼python(12)--python关键词,包
关键词 1.什么是关键词: 就是系统已经定义好的一些关键词语法,可以直接使用,很明显的就是在IDE里这些关键词会高亮显示的就是 2.有哪些关键词: 前面我们一直在说关键词,内置函数,到底有哪些关键词对 ...
- 洗礼灵魂,修炼python(14)--模块decimal, fractions,operator,collections以及精度介绍
decimal 1.作用: 用于浮点数计算.相比内置的二进制浮点数实现 float这个类型有助于 金融应用和其它需要精确十进制表达的场合,控制精度,控制舍入以适应法律或者规定要求,确保十进制数位精度, ...
- 数据库_存储过程简介(oracle版)
应朋友要求,写个存储过程说明,本篇比较简单,适合新接触存储过程的同学 先来个简单的 begin dbms_output.put_line('my first execute'); end; 如果使用的 ...
- python3 下列表与字典转换
在写爬虫的时候,经常需要处理cookie,requests库里的cookie是dict,但是headers['cookie']却是一个key=value的字符串. 下面是几个用推导式实现的转换函数,供 ...