![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
PAT
ethannotlazy
这个作者很懒,什么都没留下…
展开
-
7-1 Good in C (20分)
7-1 Good in C (20分)When your interviewer asks you to write “Hello World” using C, can you do as the following figure shows?Input Specification:Each input file contains one test case. For each case, the first part gives the 26 capital English letters A-原创 2020-07-24 23:04:24 · 155 阅读 · 0 评论 -
PAT考试总结
pat试题总结遍历问题的总结dfs中,如果是有环的图,要设置visited数组防止绕圈,同时在dfs函数退出前要将visited数组相应设置为false,否则其他路径就不能遍历该结点;在问题中,如果要求“从一个序列中选取若干个元素来满足条件”,可以考虑dfs,如1103 Integer Factorization (30分)和7-1 Forever (20分);字符串处理总结字符串处理中,注意利用sscanf,可以按照格式读取字符串中的数字,如sscanf(s, “The root is原创 2020-12-08 19:20:18 · 392 阅读 · 2 评论 -
7-1 Forever (20分)
“Forever number” is a positive integer A with K digits, satisfying the following constrains:the sum of all the digits of A is m;the sum of all the digits of A+1 is n;the greatest common divisor of m and n is a prime number which is greater than 2.Now原创 2020-07-23 21:52:43 · 163 阅读 · 1 评论 -
1149 Dangerous Goods Packaging (25point(s))
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原创 2020-07-23 08:55:21 · 108 阅读 · 0 评论 -
7-1 Sexy Primes (20分)
7-1 Sexy Primes (20分)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.I原创 2020-07-21 22:03:42 · 2263 阅读 · 0 评论 -
7-3 Telefraud Detection (25分)
7-3 Telefraud Detection (25分)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原创 2020-07-21 21:50:40 · 279 阅读 · 1 评论 -
7-4 Structure of a Binary Tree (30分)
7-4 Structure of a Binary Tree (30分)Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.Now given a sequence of statements about the stru原创 2020-07-21 21:34:48 · 186 阅读 · 0 评论 -
1104 Sum of Number Segments (20分)
注意点:由于用double类型保存十进制数时有舍入误差,数据量较大时误差积累,导致结果出错。题目说到每个数不大于1,可以先将每个数放大100000倍,相当于人为地提高double类型的精度,进而减小了误差;注意res += seq[i] * (n - i) * (i + 1);不可以写成res += (n - i) * (i + 1) * seq[i];,因为数据量为105级别的,于是(n - i) * (i + 1)最多可达1010,而int类型最大约为2×109,已经超出了int的表示范围,结果.原创 2020-07-03 23:03:35 · 148 阅读 · 0 评论 -
1099 Build A Binary Search Tree (30分)
先构造好树的结构,然后记录中序遍历下数组的下标。将给出的值排序,按照中序遍历的下标顺序将各值填入树的结点中。#include<iostream>#include<queue>#include<algorithm>#include<vector>using namespace std;struct node { int val, left, right;}table[100];vector<int> inorder;voi..原创 2020-05-22 00:38:50 · 91 阅读 · 0 评论 -
1087 All Roads Lead to Rome (30分)
常规思路,Dijkstra + dfs先用Dijkstra找出totalCost最小的所有路线,用pre数组记录他们的前驱关系。用dfs从得到的路线中找出totalHappiness最大的,或者avgHappiness最大的。注意一些细节,比如输入并没有给出起点城市的happiness。#include<iostream>#include<string>#include<unordered_map>#include<vector>usin.原创 2020-05-18 23:47:48 · 123 阅读 · 0 评论 -
1074 Reversing Linked List (25分)
用数组存结点的方法比较绕,而且不好调试:#include<algorithm>#include<iostream>using namespace std;int head, total, length;struct node { int add, val, next;}table[100010];bool isEnough(int start, int length) { int count = 0, cur = start; while (cur != -1 .原创 2020-05-13 00:22:09 · 354 阅读 · 0 评论 -
1073 Scientific Notation (20分)
#include<string>#include<math.h>#include<iostream>using namespace std;int main() { string s; cin >> s; int indexOfE = s.find('E'); string baseNumber = s.substr(1, indexOfE - 1); int exp = stoi(s.substr(indexOfE + 1)); int原创 2020-05-13 00:20:29 · 131 阅读 · 0 评论 -
1075 PAT Judge (25分)
常规思路,注意检查数组大小大于给出范围。注意点:一题都没提交的、没有一个提交通过编译器的不进入排序没提交过的题目输出-,提交过得零分,或未通过编译器输出0用-2表示从未提交,用-1表示提交过但是没有通过编译器。#include<algorithm>#include<iostream>#include<vector>#include<cmath>using namespace std;struct node { int rank, ..原创 2020-05-13 00:18:43 · 90 阅读 · 0 评论 -
1080 Graduate Admission (30分)
注意以下段代码:if (app[id2index[last]].rank == a.rank) { school[c].admitted.push_back(a.id); break;}如果学校保存的是学生的下标,那么最后输出前对学生的id排序就不方便;如果学校保存的是学生的id,这时当判断录取的最后一名和当前学生名次时,难以根据id找到学生的下标;学校的成员变量可改为vector<Applicant> admitted,这时就可以得到学生的所有信息,或者如上用unordere.原创 2020-05-13 00:00:17 · 120 阅读 · 0 评论 -
1069 The Black Hole of Numbers (20分)
用string完成比较麻烦,要将string转为数组再进行排序,然后再转化为string#include<iostream>#include<algorithm>#include<string>#include<vector>using namespace std;bool cmp(int a, int b) { return a > b;}int getVal(vector<int> s) { int res = ..原创 2020-05-10 22:10:57 · 234 阅读 · 0 评论 -
1067 Sort with Swap(0, i) (25分)
注意不要暴力查找还没排序的value。#include<iostream>#include<unordered_map>using namespace std;unordered_map<int, int> indexes;int unsortedNum = 1;int getUnsortedValue() { //don't traverse f...原创 2020-05-05 17:48:47 · 124 阅读 · 0 评论 -
1064 Complete Binary Search Tree (30分)
通过观察发现,对于既是BST又是CST的树,它的任意一个结点存在三种情况:左子树是满二叉树,但右子树不是满二叉树左子树不是满二叉树,但右子树是满二叉树左右子树都是满二叉树同时,由完全二叉树的定义可知:对于树的某个位置,它的左子树的结点数>右子树的结点数。我们可以从中序序列的中间开始向右逐个扫描,当遇到某个结点符合以上三种情况之一时就可以确定当前位置应该放置该结点。(中序序列通过对...原创 2020-04-14 17:41:51 · 107 阅读 · 0 评论 -
1062 Talent and Virtue (25分)
#include<iostream>#include<algorithm>#include<vector>using namespace std;int total, lowerBound, higherBound;struct node { int id, talent, virtue, total;};int getType(node a...原创 2020-04-13 17:10:18 · 91 阅读 · 0 评论 -
1057 Stack (30分)
用两个multiset来划分栈中的元素,s1保存较小的一半,s2保存较大的一半,当要输出中位数时直接输出s1的最后一个元素。注意multiset的用法。#include<cstdio>#include<set>#include<stack>#include<cstring>using namespace std;const int in...原创 2020-03-27 16:05:48 · 139 阅读 · 0 评论 -
1049 Counting Ones (30分)
思路:分别对每位1的出现次数进行统计#include<iostream>using namespace std;int main() { long long num, count = 0; cin >> num; //take 132 as an example long long digit = 1; //indicate which digit's one...原创 2020-03-21 17:26:09 · 74 阅读 · 0 评论 -
1047 Student List for Course (25分)
直接用string会超时,于是用C风格字符串记录姓名,注意要用char *name = new char[5]来动态创建新的字符数组。#include<string>#include<algorithm>#include<vector>#include<map>#include<iostream>using namespa...原创 2020-03-20 17:39:51 · 76 阅读 · 0 评论 -
1045 Favorite Color Stripe (30分)
用unordered_map记录目标序列中每个数字的下标,用数组seq记录要查找的序列(也就是输入样例中第三行的序列),设dp[i]表示选取seq[i]时,从序列seq[0]~seq[i]可以得到的最长的目标序列,用一个变量j从i-1起向前遍历找到的第一个满足position[j] <= position[i]的dp[j],则dp[i] == dp[j] + 1。#include<...原创 2020-03-19 18:59:45 · 83 阅读 · 0 评论 -
1026 Table Tennis (30分)
这题无疑是PAT甲级最难的一题。首先对两个实体设置结构体Player和Table用list模拟排队的队伍,用priority_queue模拟正在被占用的球桌。用一个int模拟时间的流逝,每逝去一秒依次检查:有无桌子结束游戏;有无新的玩家到达;如果队列中有玩家等候,且有空桌,按选桌规则让一个玩家开始游玩。桌子选取规则:普通玩家直接选取桌号最小的;vip玩家优先选择桌号最小的...原创 2020-03-10 23:23:56 · 96 阅读 · 0 评论 -
1022 Digital Library (30分)
为每个查询方式设置一个散列索引unordered_map<string, set<int> > index#include<iostream>#include<string>#include<algorithm>#include<unordered_map>#include<set>using na...原创 2020-03-09 16:18:55 · 75 阅读 · 0 评论 -
1021 Deepest Root (25分)
先用dfs数出图的连通分量数count,如果连通分量数count > 1直接输出结果;如果count == 1,说明该图连通,又因为图中只有n - 1条边,该图必然为无环图,对每个根节点进行dfs,找出根的最大深度。#include<cstdio>#include<unordered_set>#include<vector>#include&l...原创 2020-03-08 17:18:00 · 79 阅读 · 0 评论 -
1020 Tree Traversals (25分)
根据中序序列和后序序列的特性构造树。#include<cstdio>#include<unordered_map>#include<queue>#include<vector>using namespace std;int n, postorder[30], inorder[30];vector<int> res;uno...原创 2020-03-08 16:43:17 · 71 阅读 · 0 评论 -
1017 Queueing at Bank (25分)
用一个int变量模拟时间的流逝,每过去一秒就依次检查一下:有没有结束服务的窗口;有没有新到达的顾客;有没有空窗口,如果有,并且队列中有顾客,让该窗口接收一个顾客;注意以上顺序不能互换。为了加快检查窗口的速度,用优先队列模拟窗口;为了检查有无顾客到达,先对所有顾客到达时间进行排序。注意五点之后到达的顾客不计算在内。#include<cstdio>#include<...原创 2020-03-07 19:29:25 · 99 阅读 · 1 评论 -
1014 Waiting in Line (30分)
用队列模拟排队,用结构体记录顾客的信息:开始得到服务的时间、服务时长、结束时间。先将各窗口前的队列依次填满,之后每次从所有窗口中找出下一个结束服务的窗口。将这个窗口的下一个顾客的开始时间设为当前顾客的结束时间,如果黄线外还有更多的顾客,将一个顾客加入到这个窗口的队列中。#include<cstdio>#include<queue>using namespace s...原创 2020-03-04 16:05:10 · 113 阅读 · 0 评论 -
1013 Battle Over Cities (25分)
用dfs数出图的连通分量数。#include<cstdio>#include<algorithm>using namespace std;int cityNum, roadNum, checkNum;bool map[1000][1000];bool visited[1000];int lostCity;void dfs(int cur) { visit...原创 2020-03-03 16:11:09 · 78 阅读 · 0 评论 -
1012 The Best Rank (25分)
用结构体记录学生各科成绩、各科排名。不应根据每个查询对表进行排序,因为查询量较大。处理查询前,先根据每个成绩排序一次,然后记录相应的排名信息。用unordered_map记录id到表下标的映射,方便查找相应学生。可以进一步抽取公共代码,避免排序代码的重复。#include<cstdio>#include<algorithm>#include<ve...原创 2020-03-03 15:13:41 · 107 阅读 · 0 评论 -
1010 Radix (25分)
设给出数字十进制值为n1,待求数字十进制值为n2。虽然给出的单个字符在范围0-10或a-z内,但是待求基数不一定最高为36,待求基数可能远大于36。待求基数的最大值为n1,因为:如果n2只有一位,那么基数大小不影响n2的值,n2的基数只需要大于n2中权数最高的位。如果n2位数大于一位,那么给出的数字至少为10,它的次低位权值为n1^1,这时n2 == n1。如果基数大于n1,n2必然大...原创 2020-03-02 21:23:02 · 119 阅读 · 0 评论 -
1009 Product of Polynomials (25分)
用数组存多项式,数组下标为多项式次数,存的内容是多项式系数。#include<cstdio>double p1[1001] = { 0 }, p2[1001] = { 0 }, res[2501] = { 0 };int main() { int k1, k2, k3 = 0; scanf("%d", &k1); for (int i = 0;i < k1;...原创 2020-03-02 18:14:57 · 76 阅读 · 0 评论 -
1004 Counting Leaves (30分)
用一个结构体表示每个结点,借助队列对树进行层序遍历,记录没有子结点的结点。#include<cstdio>#include<vector>#include<queue>using namespace std;int nodeNum, nonLeaf;struct node { int val; vector<int> childre...原创 2020-02-29 23:50:51 · 68 阅读 · 0 评论 -
1003 Emergency (25分)
利用Dijkstra最短路算法,用totalWeight记录到达某个城市时最多能搜集的救援队数,用dist表示当前到达某个城市的最短距离;用pre记录某一城市的前驱结点,即从哪个城市来。最后用dfs数出最短路数。#include<cstdio>#include<algorithm>#include<vector>using namespace std;...原创 2020-02-29 23:30:46 · 91 阅读 · 0 评论 -
1002 A+B for Polynomials (25分)
因为多项式最高次只有1000,用数组保存多项式比较简单,数组下标对应多项式次数,保存的数字对应多项式系数。相加后,将系数不为0的项输出即可。#include<cstdio>int main() { double p1[1001] = { 0 }, p2[1001] = { 0 }, res[1001] = { 0 }; int n1, n2; scanf("%d", &...原创 2020-02-28 23:34:29 · 74 阅读 · 0 评论 -
1001 A+B Format (20分)
两个数字都不超过int类型的范围,直接相加。然后将结果转换为字符串,每隔三个数字插入一个逗号,注意判断有负号的情况。#include<iostream>#include<string>using namespace std;int main() { int a, b; cin >> a >> b; int sum = a + b;...原创 2020-02-28 22:29:08 · 109 阅读 · 0 评论 -
1153 Decode Registration Card of PAT (25分)
先从卡号提取信息,再根据相应信息筛选。尽量用scanf和printf进行输入输出。#include<iostream>#include<string>#include<algorithm>#include<vector>#include<unordered_map>using namespace std;struc...原创 2020-02-10 15:04:13 · 80 阅读 · 0 评论 -
1151 LCA in a Binary Tree (30分)
思路:根据先序序列和中序序列建树对于每对数字,分别记录它们的路径找到最低公共结点代码:#include<cstdio>#include<unordered_map>#include<vector>using namespace std;int pairNum, keyNum;int inorder[10001], preorder[100...原创 2020-02-09 13:13:42 · 101 阅读 · 0 评论 -
1148 Werewolf - Simple Version (20分)
两种思路:每次假设特定的两个人在说谎#include<cstdio>#include<vector>#include<unordered_set>#include<set>#include<algorithm>using namespace std;int word[102];int state[102];ve...原创 2020-02-08 15:11:06 · 194 阅读 · 0 评论 -
1145 Hashing - Average Search Time (25分)
注意:判断素数的方法继续循环条件为i * i <= num。平方探测法的增量d取值范围为0 <= d <= tableSize。通过计算样例得知,无论探测的位置是否为空,都算一次探测。#include<cstdio>int tableSize, inputNum, keyNum;int table[10100] = { 0 };bool isPri...原创 2020-02-08 11:46:16 · 122 阅读 · 0 评论