PAT甲级1140,1144解题报告

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

1140 Look-and-say Sequence (20 point(s))

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<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
vector<int> num;
int main()
{
	int D;
	int N;
	scanf("%d %d", &D, &N);
	num.push_back(D);
	for (int i = 0; i < N - 1; i++) {
		vector<int> t;
		int count = 0;
		int a = num[0];
		for (int j = 0; j < num.size(); j++) {
			if (num[j] == a)count++;
			else {
				t.push_back(a);
				t.push_back(count);
				count = 1;
				a = num[j];
			}
			if (j == num.size() - 1) {
				t.push_back(a);
				t.push_back(count);
			}
		}
		num = t;
	}
	for (int i = 0; i < num.size(); i++) {
		cout << num[i];
	}
	cout << endl;
	return 0;
}

1144 The Missing Number (20 point(s))

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤10​5​​). Then N integers are given in the next line, separated by spaces. All the numbers are in the range of int.

Output Specification:

Print in a line the smallest positive integer that is missing from the input list.

Sample Input:

10
5 -25 9 6 1 3 4 2 5 17

Sample Output:

7

题目大意:找出缺少的最小的正整数。

解题思路:很简单,从1开始每次去找数组里面有没有这个数就行了,常规的遍历肯定超时,所以用二分优化一下就好了。

#include<iostream>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<time.h>
#include<math.h>
#include<set>
#include<list>
#include<climits>
#include<queue>
#include<cstring>
#include<map>
#include<stack>
#include<string>
using namespace std;
vector<int> res;
int main()
{
	int N;
	scanf("%d", &N);
	for (int i = 0; i < N; i++) {
		int tmp;
		scanf("%d", &tmp);
		res.push_back(tmp);
	}
	sort(res.begin(), res.end());
	int q;
	for (int i = 1;; i++) {
		if (!binary_search(res.begin(), res.end(), i)) {
			q = i;
			break;
		}
	}
	printf("%d\n", q);
	return 0;
}

 

PAT——1140 Look-and-say Sequence 甲级 1140 Look-and-say Sequence题目题意题解AC代码 题目 https://pintia.cn/problem-sets/994805342720868352/problems/994805344490864640 题意 题目所给样例如下: D, D1, D111, D113, D11231, D112213111, ... 拿其中的D11231, D112213111举例,后一个是用来描述前一个的 D112213111表示前一串数字中从左到右为1个D,2个1,1个2,1个3,1个1, 阅读详情

相关推荐

PAT甲级】1140

题目链接:PAT甲级】1140 题目简述:给两个数字D和n,第⼀个序列是D,后⼀个序列描述前⼀个序列的所有数字以及这个数字出现的次数,⽐如D出现了1次,那么第⼆个序列就是D1,对于第⼆个序列D1,第三个序列这样描述: D出现1次,1出现1次,所以是D111……以此类推,输出第n个序列 #include<bits/stdc++.h> using namespace std; int main(){ string s1, s2; int N; cin >> s

qq_41375322的博客 188

PAT 1144 java 版

PAT 1144 java 版 1.题意 给出N个整数的序列,你需要找出序列中未出现的最小正整数。 2.分析 使用数组存储未被找到过的数字 使用Set粗出未被找到过的数字 3.测试用例 10 5 -25 9 6 1 3 4 2 5 17 10 5 7 9 6 1 3 4 2 8 10 4.代码 import java.io.BufferedReader; import java.io.I...

说文科技,做有态度的研究。 563

PAT甲级 - C++题解】1140 Look-and-say Sequence

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 889

PAT 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 n...

Hickey_Chen的博客 470

浙江大学计算机与软件学院2021年考研复试上机模拟练习

7-1 Square Friends (20 分) For any given positive integer n, two positive integers A and B are called Square Friends if by attaching 3 digits to every one of the n consecutive numbers starting from A, we can obtain the squares of the n consecutive numbers s

Slatter的博客 2798

PAT考试小经验

本人今天下午参加的PAT甲级考试,并且是第一次参加,满分。简要的说下考试的经验吧,首先一定先要了解考点的编译器,这个是非常重要的。由于现在PAT考试已经取消了考前试机环节,那么对编译器不是很熟悉的话,会让你的临场发挥状态变差,这会导致你发挥不出来你的固有实力。以visual studio编译器为例,说一下要注意的情况。首先不管是什么版本,先按如下图所示一步一步的来:

湛蓝的天空 4万+

PAT甲级1140

#include&amp;lt;iostream&amp;gt; #include&amp;lt;vector&amp;gt; using namespace std; vector&amp;lt;int&amp;gt; a, b, temp; int fun(vector&amp;lt;int&amp;gt; &amp;amp;temp) { int cnt = 1; for (int i = 0; i &a

宇宙-AnonyMous- 299

PAT甲级

                                                   欢迎找茬                                                                                                            作者 渣渣侠                         ...

a874288174的博客 1万+

pat甲级1003

这道题典型的求最短路径的算法,用Djktra算法写。关键点是要求出最短路径个数和最短路径中救援人员最多的人数,一开始想先求出最短距离再用DFS扫出最短路径,但在DFS中不好控制起点与终点路径扫描次序也会不同,递归的方法真的很绕人啊!在Djktra算法中可以用在线处理的方法求出最短路径个数,当出现更短的路径时“重设”救援人数和路径计数,当出现相等路径时计数增加,救援人数如果更大再更新!但发现一个问题...

brillantchen的博客 591

PAT甲级 1140 Look-and-say Sequence (20 分) 题解

\quad这个题需要读懂题意,即每一次操作为统计字符串中字符连续出现的次数,并将出现次数转化为字符串加在该字符后面,形成新的字符串。举个例子,D112111,D出现1次,接下来1连续出现2次,再接下来2出现1次,最后1连续出现3次,则结果为D1 12 21 13(为方便观看我将每个部分用空格隔开,实际字符串中无空格,即D1122113)。理解题意后解答就很容易,程序如下: #include &...

qq_40438165的博客 1434

PAT 甲级 1140 Look-and-say Sequence(20 分)

题目链接#include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;bits/stdc++.h&gt; using namespace std; int main() { int D; int n; while(~scanf("%d %d", &amp;D, &amp;n)) { ...

Q_smell的博客 423

PAT 解题报告

PAT 1070Mooncake (25)时间限制100 ms内存限制32000 kB代码长度限制16000 B判题程序Standard作者CHEN, YueMooncake is a Chinese bakery product traditionally eaten during the Mid-Autumn Festival. Many types of fillings and crust

Rachel Zhang的专栏 1万+

PAT甲级 1140. Look-and-say Sequence (20)

1140. Look-and-say Sequence (20)时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueLook-and-say sequence is a sequence of integers as the following:D, D1, D111, D113, D11231, D112213111, ... wh...

Evildoer_llc的博客 698

PAT甲级1007

1007. Maximum Subsequence Sum (25)时间限制400 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueGiven a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, .....

qq_36317016的博客 364

PAT甲级真题1140 外观数列

外观数列是指具有以下特点的整数序列: D, D1, D111, D113, D11231, D112213111, … 其中 D 是一个 [0,9][0,9] 范围内的不等于 11 的整数。 序列的第 n+1n+1 项是对第 nn 项的描述。 比如第 22 项表示第 11 项有 11 个 D,所以就是 D1; 第 22 项是 11 个 D(对应 D1)和 11 个 11(对应 1111),所以第 33 项就是 D111。 又比如第 44 项是 D113,其描述就是 11 个 D,22 个 11,11 个 3

qq_43738331的博客 223

字符串处理 pat1140

//1140 #include <iostream> #include<math.h> #include<stdio.h> #include<stdlib.h> #include<vector> #include<string> using namespace std; int main() { //fre...

BuGengFeng的博客 231

pat a1067

Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, ...

qq_41474792的博客 233

PAT 甲级】1144 The Missing Number(20 分)

题目链接Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.Input Specification:Each input file contains one test case. For each case, the first line giv...

feng_zhiyu的博客 443

PAT甲级 1140——1147

做做基础题练手,但是感觉pat难度在增加啊(是错觉嘛。。。。) 1140.Look-and-say Sequence (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Look-and-say sequence is a sequence of integers as the f...

darknight 1728
上一篇: PAT甲级1132和1136解题报告
下一篇: 微信小程序全栈(一).通过授权登录解密获取用户的openid和unionid
Tate_Brown
博客等级 码龄9年 39粉丝 107原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值