PAT刷题记录
MaxLeo928
随缘记录
展开
-
PAT A 刷题说明 (附近年教育超市题目题解链接)
备考2021.9 PAT A刷到哪记录到哪,之前有些刷过但没记录的,后面有空补上;使用C C++混合解题,学习的是算法笔记的写法;加油冲冲冲原创 2021-08-08 22:41:42 · 223 阅读 · 0 评论 -
PAT A 2021年春 7-4 Recycling of Shared Bicycles (30 分) AC代码(Floyd算法)
7-4 Recycling of Shared Bicycles (30 分)There are many spots for parking the shared bicycles in Hangzhou. When some of the bicycles are broken, the management center will receive a message for sending a truck to collect them. Now given the map of city, yo原创 2021-09-11 21:05:12 · 180 阅读 · 0 评论 -
PAT A 2021年春 7-3 Structure of Max-Heap (25 分) AC代码(堆的插入,向上调整)
7-3 Structure of Max-Heap (25 分)In computer science, amax-heapis a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is greater than or equal to the key of C. A common impl..原创 2021-09-11 21:02:33 · 106 阅读 · 0 评论 -
PAT A 2021年春 7-2 Lab Access Scheduling (25 分) AC代码(区间贪心)
7-2 Lab Access Scheduling (25 分)Nowadays, we have to keep a safe social distance to stop the spread of virus due to the COVID-19 outbreak. Consequently, the access to a national lab is highly restricted. Everyone has to submit a request for lab use in ad原创 2021-09-11 20:59:13 · 100 阅读 · 0 评论 -
PAT A 2020年春 7-4 Replacement Selection (30 分) AC代码
7-4 Replacement Selection (30 分)When the input is much too large to fit into memory, we have to doexternal sortinginstead of internal sorting. One of the key steps in external sorting is to generate sets of sorted records (also calledruns) with limite...原创 2021-09-10 23:06:58 · 189 阅读 · 0 评论 -
PAT A 2020年冬 7-3 File Path AC代码
7-3 File Path (25 分)The figure shows the tree view of directories in Windows File Explorer. When a file is selected, there is a file path shown in the above navigation bar. Now given a tree view of directories, your job is to print the file path for an原创 2021-09-09 10:35:37 · 99 阅读 · 0 评论 -
PAT A 2020年冬 7-2 Subsequence in Substring AC代码
7-2 Subsequence in Substring (25 分)Asubstringis a continuous part of a string. Asubsequenceis the part of a string that might be continuous or not but the order of the elements is maintained. For example, given the stringatpaaabpabtt,pabtis a subs...原创 2021-09-09 10:31:06 · 80 阅读 · 0 评论 -
PAT A 2020年冬 7-1 The Closest Fibonacci Number AC代码
7-1 The Closest Fibonacci Number (20 分)TheFibonacci sequenceFnis defined byFn+2=Fn+1+Fnforn≥0, withF0=0andF1=1. Theclosest Fibonacci numberis defined as the Fibonacci number with the smallest absolute difference with the given integerN....原创 2021-09-09 10:29:22 · 88 阅读 · 0 评论 -
PAT 2020年秋 7-4 Professional Ability Test (30 分) AC代码(拓扑排序以及DFS)
7-4 Professional Ability Test (30 分)Professional Ability Test (PAT) consists of several series of subject tests. Each test is divided into several levels. Level A is aprerequisite (前置要求)of Level B if one must pass Level A with a score no less thanSin...原创 2021-09-06 22:10:15 · 521 阅读 · 0 评论 -
PAT 2020年秋 7-3 Left-View of Binary Tree (25 分) AC代码
7-3 Left-View of Binary Tree (25 分)Theleft-viewof a binary tree is a list of nodes obtained by looking at the tree from left hand side and from top down. For example, given a tree shown by the figure, its left-view is { 1, 2, 3, 4, 5 }Given the ino..原创 2021-09-06 22:05:08 · 173 阅读 · 1 评论 -
PAT 2020年秋 7-2 How Many Ways to Buy a Piece of Land (25 分) AC代码
7-2 How Many Ways to Buy a Piece of Land (25 分)The land is for sale in CyberCity, and is divided into several pieces. Here it is assumed that each piece of land has exactly two neighboring pieces, except the first and the last that have only one. One can原创 2021-09-06 22:02:45 · 121 阅读 · 0 评论 -
PAT 2020年秋 7-1 Panda and PP Milk (20 分) AC代码
7-1 Panda and PP Milk (20 分)PP milk (盆盆奶)is Pandas' favorite. They would line up to enjoy it as show in the picture. On the other hand, they could drink in peace only if they believe that the amount of PP milk is fairly distributed, that is, fatter pan原创 2021-09-06 21:59:16 · 247 阅读 · 3 评论 -
PAT 2020年春 7-3 Safari Park AC代码
7-3 Safari Park (25 分)A safari park(野生动物园)hasKspecies of animals, and is divided intoNregions. The managers hope to spread the animals to all the regions, but not the same animals in the two neighboring regions. Of course, they also realize that this...原创 2021-09-06 21:43:15 · 169 阅读 · 0 评论 -
PAT 2020年春 7-2 The Judger AC代码(使用unordered_set)
7-2 The Judger (25 分)A game of numbers has the following rules: at the beginning, two distinct positive integers are given by the judge. Then each player in turn must give a number to the judge. The number must be the difference of two numbers that are p原创 2021-09-06 21:39:39 · 94 阅读 · 0 评论 -
PAT 2020年春 7-1 Prime Day AC代码
7-1 Prime Day (20 分)The above picture is from Sina Weibo, showing May 23rd, 2019 as a very cool "Prime Day". That is, not only that the corresponding number of the date20190523is a prime, but all its sub-strings ended at the last digit3are prime numb...原创 2021-09-06 21:35:02 · 114 阅读 · 0 评论 -
PAT 2019年冬 7-4 Cartesian Tree AC代码
根据题目描述,tree函数根据给出的中序和性质遍历树,然后每层记录在le[]中#include<iostream>#include<vector>using namespace std;vector<int> in,le[30];int N;void tree(int root,int start,int end,int level){ if(start>end) return; le[level].push_back(in[root]);..原创 2021-09-06 15:21:49 · 80 阅读 · 0 评论 -
PAT 2019年冬 7-3 Summit AC代码
对于每个人,我用了一个set记录他的好友#include<iostream>#include<vector>#include<set>using namespace std;static set<int> s[210];static set<int> f;int N,M,K;int main(){ cin>>N>>M; for(int i=0;i<M;i++){ int a,b; s..原创 2021-09-06 15:14:45 · 106 阅读 · 0 评论 -
PAT 2019年冬 7-2 Block Reversing AC代码
用algorithm中的reverse函数比较方便#include<iostream>#include<vector>#include<algorithm>using namespace std;int start,N,K;vector<int> L,ans;int cnt=0;struct node{ int data,next;}Node[100000];void trail(int index){ if(index==-1原创 2021-09-06 15:08:02 · 75 阅读 · 0 评论 -
PAT A 2019年秋季 7-4 Dijkstra Sequence AC代码
一道dijkstra算法的模板题对于所谓的Dijkstra Sequence,直接判断给出结点序列到起点距离(d[x])是否是不严格增序即可#include<iostream>#include<vector>using namespace std;int Nv,Ne,K;int inf=1000000000;int G[1001][1001];int d[1001];void dijkstra(int s){ fill(d,d+1001,inf);原创 2021-09-01 22:10:30 · 129 阅读 · 0 评论 -
PAT A 2019年秋季 7-3 Postfix Expression AC代码
对于如题给出的语法树,每个结点只有三种情况:1、左子不空,右子不空2、左子空,右子不空3、左子空,右子空根据题意,对应的输出应为1、( 左子树内容 右子树内容 当前结点内容 )2、(当前结点内容 右子树内容 )3、(当前结点内容)按照这个去递归语法树即可tips:我一开始根据表达式可能的情况,对加乘除以及减四个符号分开专门判断,最后一个测试点没法通过。说不定最后一个测试点的符号不一定是题中给出的符号样式或者输出是一个不合法的后缀表达式。(个人猜测) 所以抓住语法树的结构来原创 2021-09-01 22:07:12 · 160 阅读 · 0 评论 -
PAT A 2019年秋 7-2 Merging Linked Lists AC代码
我就按照题目意思,先用结构体记录链表,然后遍历短的(go1函数)(先从b1跑一遍,如果记录下的数量>N/2,说明b1开始的是长的,那么就重新跑a1),记录下短的地址顺序,再去跑长的,边跑边记录并插入短的(go函数)最后按照格式输出#include<iostream>#include<vector>#include<algorithm>using namespace std;int a1,b1,N;struct node{ int num,n原创 2021-09-01 21:55:38 · 176 阅读 · 0 评论 -
PAT A 2019年秋 7-1 Forever AC代码
这道题很搞心态,用了素数判断、最大公约数以及各数位求和(分别写为了3个函数)但是对范围内的数字进行暴力遍历的话,最后两个测试点会超时需要根据测试结果想到所有满足条件的数都是以99结尾,这样只遍历99结尾的数才不会超时(其实不大好想到,要多试一些数据,还有题目的名字联想)#include<iostream>#include<cmath>#include<vector>#include<algorithm>using names.原创 2021-09-01 21:50:12 · 83 阅读 · 0 评论 -
PAT A 2019春 7-4 Structure of a Binary Tree AC代码
一开始看到题目的时候有点懵,这个输入有点让人头大按照种类不断细分来写,对于每个输入一个一个词读取判断还有就是根据后序和中序建树,为方便判断结构体多存一点信息我根据后序的顺序对每个数字编号,用了一个map记录对应关系#include<iostream>#include<vector>#include<unordered_map>using namespace std;int N,M;vector<int> post,in;.原创 2021-09-01 17:12:04 · 65 阅读 · 0 评论 -
PAT A 2019春 7-3 Telefraud Detection AC代码
题目稍微有点复杂最好用并查集,形成gang第一次写输出的时候看错题目了,题目只要求按照gang的首个数字大小输出,不要求按照gang的人数排序,读题目要再小心一点#include<iostream>#include<vector>using namespace std;int K,N,M;int t[1001][1001]={0};int father[1001];int findfather(int x){ if(father[x]==x){.原创 2021-09-01 17:07:19 · 142 阅读 · 0 评论 -
PAT A 2019年春 7-2 Anniversary AC代码
用了一个set记录校友,读取来宾的时候做两件事:1、判断是否是校友,是则ans1++2、判断是否是最年长的,是则更新ans2和生日最小值#include<iostream>#include<set>#include<algorithm>using namespace std;int N,M;int ans1=0,Min=100000000;string ans2;set<string> alumni;int main().原创 2021-09-01 15:13:08 · 87 阅读 · 0 评论 -
PAT A 2019年春 7-1 Sexy Primes AC代码
主要就是素数判断,注意一下往前判断的时候加一个>0的条件#include<iostream>#include<cmath>using namespace std;int N;bool is_prime(int x){ int k=(int)sqrt(x*1.0); if(x==0||x==1) return false; for(int i=2;i<=k;i++){ if(x%i==0) return false; } return tr...原创 2021-09-01 15:06:47 · 94 阅读 · 0 评论 -
PAT A 1155 AC代码(堆的遍历,路径记录)
check函数遍历堆,用path记录路径,在遍历到叶结点(叶结点条件:2*index>N) 时输出。遍历的时候顺便比较一下父子大小判断是大顶堆还是小顶堆#include<iostream>#include<vector>using namespace std;int N;vector<int> heap,path;bool Max=true,Min=true;void check(int index){ if(index>N){ r原创 2021-08-31 16:01:24 · 47 阅读 · 0 评论 -
PAT A 1154 AC代码
根据给出的颜色判断一下每条边两端是否是不同颜色就好了。简单题#include<iostream>#include<vector>#include<set>using namespace std;vector<int> edge;int N,M,K;int main(){ cin>>N>>M; edge.resize(2*M); for(int i=0;i<M;i++){ scanf("%d %d",&a原创 2021-08-31 15:33:31 · 67 阅读 · 0 评论 -
PAT A 1153 AC代码(耗时100ms以内)
在这种数据处理复杂并且可能输出较多的情况下!!!牢记:多次读取,多次输出的地方(循环中),能用scanf和printf就不要用cin和cout这道题就是典型的换输出方法会导致耗时非常显著变化的例子最终的耗时非常可观,都控制在100ms以内,远远低于200的限制使用了多个map来避免每次都对所有记录遍历,读取数据的时候就完成数据处理写的时候为了每种情况写得更加直观,设置的类型比较多#include<iostream>#include<vector>..原创 2021-08-30 23:41:11 · 70 阅读 · 0 评论 -
PAT A 1152 AC代码
#include<iostream>#include<cmath>using namespace std;int L,K;string s;bool is_prime(int x){ int k=(int)sqrt(x*1.0); if(x<=1) return false; for(int i=2;i<=k;i++){ if(x%i==0) return false; } return true;}int main(){ cin>&.原创 2021-08-28 22:32:19 · 67 阅读 · 0 评论 -
PAT A 1151 AC代码(LCA问题的通法思路)
这题可以结合1143来学习理解。树LCA解法通法思路:确保U、V均存在于树上后从根结点开始1、若当前结点index的中序位置在U,V的中序位置之间,则当前结点index就是U、V的LCA2、若U、V的中序位置均小于index的中序位置,index->当前结点的左子,重复1-3步3、若U、V的中序位置均大于index的中序位置,index->当前结点的右子,重复1-3步实际实现的时候不用建树,要熟悉树的遍历和遍历转换,理解之后才比较好写。#include<i原创 2021-08-28 21:53:08 · 82 阅读 · 0 评论 -
PAT A 1150 AC代码
存储好数据后根据条件逐一判断即可。注意一下各种输出格式。#include<iostream>#include<vector>#include<algorithm>using namespace std;int dis[201][201];int N,M,K,min_num=0,Min=1000000000;int main(){ cin>>N>>M; fill(dis[0],dis[0]+201*201,-1); for(i原创 2021-08-28 21:10:23 · 51 阅读 · 0 评论 -
PAT A 1149 AC代码(散列,两种解法)
解法一:用一个map<int,vector<int>> G 标记每个物品对应的不相容物品引入一个ban数组,用来标记出现到第j个物品时,都有哪些物品不能出现,如果第j个物品可以出现,将其不相容物品在ban中标记为1,否则,flag=false;#include<iostream>#include<vector>#include<unordered_map>using namespace std;int N,M;unorder原创 2021-08-28 20:33:47 · 78 阅读 · 0 评论 -
PAT A 1148 AC代码(两种可行思路和代码)
应该是20分题里面容易出问题的题目了。思路一(可行但是复杂,不推荐,推荐下面的思路二):拿到题目我的第一思路是:分别假设两个人撒谎,反转撒谎人的话,然后根据他们的话去确定身份,再在不违背已知身份的条件下,假设说谎人身份一狼一好,然后统计符合狼数条件的情况。!!!要注意的问题:根据所有人的话,再加上你不违背话的假设,无法确定每个人的身份,有的人是好人,有的人是狼人,还有部分是无法确定的身份,这个时候会出现两种可能满足的情况:1、遍历后发现,有两只狼,满足所有条件,将狼号存入ans2、遍历后原创 2021-08-28 18:26:45 · 97 阅读 · 0 评论 -
PAT A 1147 AC代码(堆,大顶堆小顶堆判断,后序遍历)
判断和后序遍历分别写成了两个单独函数。按照堆的定义做即可。#include<iostream>#include<vector>using namespace std;vector<int> heap,post;int M,N;bool Max,Min;void panduan(int index){ if(index>N) return; if((2*index<=N&&heap[2*index]>heap[in原创 2021-08-26 17:09:08 · 87 阅读 · 0 评论 -
PAT A 1146 AC代码
拓扑排序的简单模拟 都不需要用队列只需要按照顺序,判断入度是否为0即可(过程中指向点的入度要跟着减少)#include<iostream>#include<vector>using namespace std;struct node{ int in; vector<int> cl;};int N,M,K;vector<int> ans;int main(){ cin>>N>>M; vector<no原创 2021-08-26 16:30:25 · 54 阅读 · 0 评论 -
PAT A 1145 AC代码(hash表及正向平方探查法)
注意一下正向平方探查法的部分t=(Key+j*j)%Msize其中0<=j<=Msize#include<iostream>#include<cmath>using namespace std;bool is_prime(int x){ if(x==2) return true; int k=(int)sqrt(x*1.0); for(int i=2;i<=k;i++){ if(x%i==0) return false; } ret原创 2021-08-26 16:15:10 · 52 阅读 · 0 评论 -
PAT A 1144 AC代码
#include<iostream>#include<vector>#include<algorithm>using namespace std;int main(){ int N; cin>>N; int ans=1; vector<int> v; for(int i=0;i<N;i++){ int x; scanf("%d",&x); if(x>0) v.push_back(x); } s.原创 2021-08-26 15:38:31 · 64 阅读 · 0 评论 -
PAT A 1143 AC代码(BST树LCA问题算法详解)
首先要知道BST树中序序列即数据点增序序列。BST树LCA解法思路理解(画个BST树就很好理解了):确保U、V均存在于树上后从根结点开始1、若当前结点index大小在U,V之间,则当前结点index就是U、V的LCA2、若U、V均小于index,index=当前结点的左子,重复1-3步3、若U、V均大于index,index=当前结点的右子,重复1-3步所以我按这个思路,配合先序和中序递归来做(测试点四超时了)原始代码:#include<iostream>原创 2021-08-25 23:05:21 · 356 阅读 · 0 评论 -
PAT A 1142 AC代码
最大点数就200,可以放心地用循环、按照判断依据写就好了,注意一下判断的逻辑就行#include<iostream>#include<vector>using namespace std;int edge[201][201]={0};int Nv,Ne,M,K;int main(){ cin>>Nv>>Ne; for(int i=0;i<Ne;i++){ int a,b; scanf("%d %d",&a,&原创 2021-08-25 21:44:28 · 76 阅读 · 0 评论