![](https://img-blog.csdnimg.cn/20190918140158853.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
PAT
文章平均质量分 79
PAT做过的题解,浙大上岸啦!
烧烤大团子
拒绝摆烂,跑起来才会有风
展开
-
A1151 LCA in a Binary Tree(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【LCA+树】
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.Given any two nodes in a binary tree, you are supposed to find their LCA.Input Specification:Each input file contains one test cas原创 2022-08-29 17:08:53 · 278 阅读 · 0 评论 -
A1150 Travelling Salesman Problem(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【旅行商问题+set】
主要是注意各个类型之间的逻辑判断顺序在记录每一个城市的访问次数的时候,之前用的是用数组visit来记录每个城市的次数,但是看了柳神的题解之后发现了更好的方法,就是用集合set来记录,这里利用了集合的特性,重复元素不会重复计算,刚好符合需求,并且在判断的时候更方便。...原创 2022-08-28 16:37:24 · 1334 阅读 · 0 评论 -
A1149 Dangerous Goods Packaging(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【map】
When shipping goods with containers, we have to be careful not to pack some incompatible goods into the same container, or we might get ourselves in serious trouble. For example, oxidizing agent (氧化剂) must not be packed with flammable liquid (易燃液体), or it原创 2022-08-23 17:14:26 · 318 阅读 · 0 评论 -
A1147 Heaps(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【最大最小堆】
In computer science, aheapis 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 either greater than or equal to (in a max heap) or less than or equal to (in a min heap) t..原创 2022-08-11 17:04:27 · 214 阅读 · 0 评论 -
A1145 Hashing - Average Search Time(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【数字处理】
The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether原创 2022-08-09 15:50:11 · 249 阅读 · 1 评论 -
A1144 The Missing Number(20分)PAT 甲级(Advanced Level) Practice(C++)满分题解【数字处理】
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 (≤105). Then N integers are given原创 2022-08-09 15:45:44 · 133 阅读 · 0 评论 -
A1143 Lowest Common Ancestor(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【常规思路+前序中序建树+LCA】
这道题给出了前序序列,然后要找给出的两个节点的LCA,又由于是BST二叉搜索树,所以key值的从小到大序列就是中序序列,所以很自然地想到根据前序和中序序列来构建BST树找节点a和b的LCA的时候主要分为以下三种情况:a和b分布在当前root的左右两边,root即为LCAa和b都在当前root的左子树,以root.left为新的root继续进行递归a和b都在当前root的右子树,以root.right为新的root继续进行递归...原创 2022-08-09 11:53:03 · 290 阅读 · 0 评论 -
A1142 Maximal Clique(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【图+极大团】
这道题首先在于看懂题目,判断输入的图是否是一个两两连通的团,或者是否是极大团,最后总结归纳为以下两个条件:是否两两相连是否存在每个人都有的共同邻居然后分别定义两个判断上述条件的bool类型函数,再根据要求在输出时进行判断...原创 2022-08-08 16:00:34 · 92 阅读 · 0 评论 -
A1141 PAT Ranking of Institutions(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【map+多条件排序】
After each PAT, the PAT Center will announce the ranking of institutions based on their students' performances. Now you are asked to generate the ranklist.Input Specification:Each input file contains one test case. For each case, the first line gives a原创 2022-08-07 16:30:35 · 469 阅读 · 0 评论 -
A1140 Look-and-say Sequence(20分)PAT 甲级(Advanced Level) Practice(C++)满分题解【字符串处理】
whereDis 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 oneDin the 1st number, and hence it isD1; the 2nd number consists of oneD(corresponding toD1) and one 1 (cor...原创 2022-08-05 17:06:31 · 174 阅读 · 0 评论 -
A1023 Have Fun with Numbers(20分)PAT 甲级(Advanced Level) Practice(C++)满分题解【大整数相加】
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a diffe原创 2022-08-05 11:39:00 · 117 阅读 · 0 评论 -
A1034 Head of a Gang(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【DFS+图的遍历】
One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call betweenAandB, we say thatAandBis related. The weight of a relation is defined to be the total time length of all the phone calls made between...原创 2022-08-04 17:06:44 · 124 阅读 · 0 评论 -
A1040 Longest Symmetric String(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【对称字符串+动态规划】
Given a string, you are supposed to output the length of the longest symmetric sub-string. For example, givenIs PAT&TAP symmetric?, the longest symmetric sub-string iss PAT&TAP s, hence you must output11.Input Specification:Each input file contains o...原创 2022-08-04 10:47:53 · 90 阅读 · 0 评论 -
A1081 Rational Sum(20分)PAT 甲级(Advanced Level) Practice(C++)满分题解【分数运算]
分数运算,设置结构体包含分数的分母和分子,然后按固定套路写就好了,注意分子分母的数据类型都是long int,但是最后结果输出的时候要输出int类型。原创 2022-08-03 15:58:15 · 71 阅读 · 0 评论 -
A1052 Linked List Sorting(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【链表地址+排序]
先将原始输入数据存入srclist中,然后根据节点的address和next来遍历找出相连接的节点存入linked_list链表,然后对linked_list根据key值从小到大的顺序排序,并记录有效节点的个数,最后再进行输出。注意输出时要更新节点的next地址值,为linked_list链表中下一个节点的address地址值。但是仅作上述操作后测试点3会fail,后来看了其他人的题解后,发现要注意当输入的起始地址start为-1时,此时应该要输出“0 -1”,加上这个判断后就AC了。...原创 2022-08-02 10:49:22 · 116 阅读 · 0 评论 -
A1032 Sharing(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【字符串+结构体+map]
首先看到这类题第一反应是定义一个结构体存放每个字母的信息,为了不受整数的各种影响,直接全部用字符串处理。基本思路是首先存放所有字母节点,然后遍历分别找出两个单词的首字母位置,然后定义createWord函数找该单词所有下一个字母位置组成序列。两个单词全部找到后,从后往前比较找相同的字母,记录最后一次相同字母的位置输出,找不到就输出“-1”。这里为了方便判断两个单词即两个结构体变量是否相同,对==运算符进行函数重载。...原创 2022-07-29 17:53:01 · 651 阅读 · 0 评论 -
A1030 Travel Plan(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【Dijkstra+DFS]
采用了Dijkstra算法和深度优先遍历DFS,用root数组存放从设定的起始点到每个点i的最短路径。题目要求是首先找到最短路径,如果最短路径的解不止一个,就再找到花费代价最小的。原创 2022-07-29 16:41:16 · 457 阅读 · 0 评论 -
A1024 Palindromic Number(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【回文串+大整数运算】
由于不断的翻转相加,最后变成了大整数运算,会溢出,所以转换为string字符串类型来计算,主要用到了reverse()函数来处理字符串的翻转,注意这个。大整数相加函数add()的写法。原创 2022-07-27 22:16:54 · 182 阅读 · 0 评论 -
A1104 Sum of Number Segments(20分)PAT 甲级(Advanced Level) Practice(C++)满分题解【子序列数学问题+测试点2注意double带来的误差】
因为输入为十进制小数,存储到double中时,计算机内部使用二进制表示,且计算机的字长有限,有的十进制浮点数使用二进制无法精确表示只能无限接近,在字长的限制下不可避免会产生舍入误差,这些细微的误差在N较大时多次累加会产生较大误差,所以建议不要使用double类型进行多次累加的精确计算,而是转为能够精确存储的整型。再举几个例子,这里不再赘述。该数所在位置序号i*(n-i+1)double类型的值多次累加导致的精度误差。这道题第一反应就是找规律总结公式。...原创 2022-07-27 18:03:33 · 230 阅读 · 0 评论 -
A1078 Hashing(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【哈希表】
从发生冲突的单元开始探查,依次查看下一个单元是否为空,如果到了最后一个单元还是空,那么再从表首依次判断。这道题的思路还算是比较明晰,将数字一个个存入到用来表示哈希表的vector中,每放一个就做标记,被放过的格子就要跳过,这里注意题目要求的是使用。定义两个散列函数,分别为s1和s2,s1的算法和前面一致,s2取一个1~m-1之间并和m互为素数的数。从发生冲突的单元加上1^2,2^2,3^2,...,n^2,直到遇到空闲的单元。从发生冲突的单元加上1^2,2^2,3^2,...,n^2,直到遇到空闲的单元。.原创 2022-07-27 16:37:13 · 501 阅读 · 0 评论 -
A1077 Kuchiguse(20分)PAT 甲级(Advanced Level) Practice(C++)满分题解【字符串】
这题还算比较简单,就是从后往前遍历找出所有字符串句子的相同部分,然后反转后输出,唯一要注意的点就是在输入字符串时,直接用cin是没有办法读入空格的,所以会出现不会输入所有字符串的情况,所以要用getline(cin,str),不过由于前面用cin输入了一个n,所以要用cin.ignore(1)忽略掉前一个cin遗留下的空格。...原创 2022-07-26 16:33:11 · 311 阅读 · 0 评论 -
A1076 Forwards on Weibo(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【BFS】
题目给出的是每个用户关注的人,所以在储存到数组时应该转换为每个人的粉丝群体,这样才能直接表示一个人可以带来的转发量,然后从查询用户为起点,该用户的粉丝列表,然后粉丝列表的粉丝列表,当层数超过L后就不计算了。...原创 2022-07-26 14:47:01 · 227 阅读 · 0 评论 -
A1126 Eulerian Path(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【欧拉图】
In graph theory, an Eulerian path is a path in a graph which visits every edge exactly once. Similarly, an Eulerian circuit is an Eulerian path which starts and ends on the same vertex. They were first discussed by Leonhard Euler while solving the famous S原创 2022-07-25 21:03:57 · 100 阅读 · 0 评论 -
A1124 Raffle for Weibo Followers(20分)PAT 甲级(Advanced Level) Practice(C++)满分题解【map】
用map来存放每个用户是否获奖,注意当一个是因为上一个人已经获奖了而递补到的获奖,那么下一轮获奖就要以当前这个人为基准进行每隔n个人获奖,而不是还是延续初始时的那个轮次。原创 2022-07-24 15:48:41 · 175 阅读 · 0 评论 -
A1125 Chain the Ropes(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【排序】
题目要求是要求所有绳索连接起来的最大长度,注意每次进行两个绳索连接操作的话,该两条绳索的长度之和就要除以2,并舍去小数,所以如果要使最后的和最大,那么就要让最长的绳索做的折半次数最少,故在获取所有绳索后先做一个从小到大的排序处理,从小到大依次折半,越小的折半后损失的长度最小。......原创 2022-07-24 15:42:48 · 119 阅读 · 0 评论 -
A1138 Postorder Traversal(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【前中后序树的遍历】
Suppose that all the keys in a binary tree are distinct positive integers. Given the preorder and inorder traversal sequences, you are supposed to output the first number of the postorder traversal sequence of the corresponding binary tree.Each input file原创 2022-06-04 22:19:17 · 64 阅读 · 0 评论 -
A1146 Topological Order(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【拓扑排序】
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.Each input file contains one test c原创 2022-06-04 21:25:01 · 114 阅读 · 0 评论 -
A1153 Decode Registration Card of PAT(25分)PAT甲级 (Advanced Level) Practice(C++)满分题解【unordered_map+排序】
A registration card number of PAT consists of 4 parts:Now given a set of registration card numbers and the scores of the card owners, you are supposed to output the various statistics according to the given queries.Each input file contains one test case. F原创 2022-06-03 22:38:06 · 109 阅读 · 0 评论 -
A1152 Google Recruitment(20分)PAT甲级 (Advanced Level) Practice(C++)满分题解【素数+字符串】
In July 2004, Google posted on a giant billboard along Highway 101 in Silicon Valley (shown in the picture below) for recruitment. The content is super-simple, a URL consisting of the first 10-digit prime found in consecutive digits of the natural constant原创 2022-06-03 17:38:06 · 1641 阅读 · 0 评论 -
A1158 Telefraud Detection(25分)PAT甲级 (Advanced Level) Practice(C++)满分题解【并查集】
Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call re原创 2022-06-03 11:48:30 · 263 阅读 · 0 评论 -
A1157 Anniversary(25分)PAT甲级 (Advanced Level) Practice(C++)满分题解【字符串+排序】
Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebration, the alumni association (校友会) has gathered the ID's of all her alumni. Now your job is to write a program to count the number of alumni among all the pe原创 2022-06-03 00:22:49 · 473 阅读 · 0 评论 -
A1166 Summit(25分)PAT甲级 (Advanced Level) Practice(C++)满分题解【图】
A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of everyone.Now given a set of tentative原创 2022-06-02 23:09:42 · 166 阅读 · 0 评论 -
A1156 Sexy Primes(20分)PAT 甲级(Advanced Level) Practice(C++)满分题解【素数】
Sexy primes are pairs of primes of the form (p, p+6), so-named since "sex" is the Latin word for "six". (Quoted from http://mathworld.wolfram.com/SexyPrimes.html)Now given an integer, you are supposed to tell if it is a sexy prime.Each input file contains原创 2022-06-02 22:34:31 · 216 阅读 · 0 评论 -
A1165 Block Reversing(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【链表反转】
Given a singly linked list L. Let us consider every K nodes as a block (if there are less than K nodes at the end of the list, the rest of the nodes are still considered as a block). Your job is to reverse all the blocks in L. For example, given L as 1→2→3原创 2022-06-02 10:59:11 · 131 阅读 · 0 评论 -
A1161 Merging Linked Lists(25分)PAT甲级 (Advanced Level) Practice(C++)满分题解【链表】
Given two singly linked lists L1=a1→a2→⋯→an−1→an and L2=b1→b2→⋯→bm−1→bm. If n≥2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a1→a2→bm→a3→a4→bm−1⋯. For example, given one list being 6→7 an原创 2022-06-02 09:36:54 · 617 阅读 · 0 评论 -
A1167 Cartesian Tree(30分)PAT甲级 (Advanced Level) Practice(C++)满分题解【最小堆+构建树+层序遍历】
A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 10, 18, 6 }, the min-heap Cartesia原创 2022-06-01 22:10:27 · 88 阅读 · 0 评论 -
A1164 Good in C(20分)PAT甲级 (Advanced Level) Practice(C++)满分题解【字符相关】
When your interviewer asks you to write "Hello World" using C, can you do as the following figure shows?Each input file contains one test case. For each case, the first part gives the 26 capital English letters A-Z, each in a 7×5 matrix of 's and 's. Then原创 2022-06-01 21:13:01 · 199 阅读 · 0 评论 -
A1163 Dijkstra Sequence(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【Dijkstra】
Dijkstra's algorithm is one of the very famous greedy algorithms.It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived原创 2022-06-01 18:22:51 · 134 阅读 · 0 评论 -
A1102 Invert a Binary Tree(25分)PAT 甲级(Advanced Level) Practice(C++)满分题解【反转二叉树+层序遍历+中序遍历】
The following is from Max Howell @twitter:Now it's your turn to prove that YOU CAN invert a binary tree!Each input file contains one test case. For each case, the first line gives a positive integer N (≤10) which is the total number of nodes in the tree原创 2022-06-01 15:41:51 · 124 阅读 · 0 评论 -
A1162 Postfix Expression(25分)PAT甲级 (Advanced Level) Practice(C++)满分题解【后缀表达式+树的遍历】
Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.Each input file contains one test case. For each case, the first line gives a positive integer N (≤原创 2022-06-01 14:24:08 · 295 阅读 · 0 评论