[PAT] 1140 Look-and-say Sequence(20 分)
1140 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
题意:
D是0到9中除1以外的数字。第n+1个数字是第n个数字的一种描述。举个例子,第二个数表示第一个数中只有一个D,因此为D1;第二个数由一个D(D1)和一个1(11)组成,因此第三个数是D111;第四个数是D113,它由一个D(D1),三个1(13)组成;下一个是D11231。
你被要求计算第n个数对于给定的D。
思路:
用一个vector接收。每一次从头开始遍历。
注意一下vector数组结尾时的数据也要进行保存。
题解:
#include<cstdlib>
#include<cstdio>
#include<vector>
using namespace std;
int main() {
int d, n;
scanf("%d %d", &d, &n);
vector<int> num;
num.push_back(d);
; i < n; i++) {
vector<int> temp;
];
;
; j < num.size(); j++) {
if (num[j] == v) cnt++;
else{
temp.push_back(v);
temp.push_back(cnt);
cnt = ;
v = num[j];
}
//如果到达结尾时,要最后一组数据进行push
) {
temp.push_back(v);
temp.push_back(cnt);
}
}
num = temp;
}
; i < num.size(); i++) {
printf("%d", num[i]);
}
;
}
[PAT] 1140 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 ...
随机推荐
- CodeForces - 50A Domino piling (贪心+递归)
CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...
- 从零开始学Linux系统(一)之引导流程解析
Linux系统:分时多用户多任务的操作系统: Linux系统引导流程: inittab配置文件中: 定义了linux系统的运行的7个级别:从0~6 0.6:分别代表关机和重启,不建议设置为默认的运行级 ...
- props设置state误区
class Component extends React.Component { constructor(props) { super(props); this.state = { value: t ...
- SRM16 B-2(DP)
老鼠和洞按坐标排序 f[i][j]表示前i个洞进j只老鼠的最短距离 比赛的时候强行分三类去推式子,推是推出来了,也看出来是可以用三个单调队列去优化的,但是太繁琐了,要我敲我真没办法T^T 赛后经 on ...
- JavaScript身份证号码有效性验证
最近需要对身份证合法性进行验证,实名验证是不指望了,不过原来的验证规则太过简单,只是简单的验证了身份证长度,现在业务需要加强下身份证验证规则,网上找到了不少资料,不过都不合偶的心意,无奈只好直接写一个 ...
- c++ string写时复制
string写时复制:将字符串str1赋值给str2后,除非str1的内容已经被改变,否则str2和str1共享内存.当str1被修改之后,stl才为str2开辟内存空间,并初始化. #include ...
- bzoj 1123 [POI2008]BLO Tarjan求割点
[POI2008]BLO Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1540 Solved: 711[Submit][Status][Discu ...
- each jquery
<div class="first"> <span>投保人数:</span> <input type="text" i ...
- Linux常用命令汇总(持续更新中)
命令 说明 注意点 cat access.log | wc -l 统计行数 awk命令可以做到同样的想过:cat access.log | awk 'END {print NR}' grep vnc ...
- CCD和CMOS的差别
单从感光器电子技术上来说,CCD比CMOS更先进,理论成像上有优势,但是最近几年CMOS却发展更好,使得很多高端数码单反采用CMOS传感器,下面来看看CCD和CMOS的技术知识: CCD和CMOS传感 ...