
C++/PAT
快乐de约定
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
[C++] PAT 1048 Find Coins (25分)
Sample Input 1: 8 15 1 2 8 7 2 4 11 15 Sample Output 1: 4 11 Sample Input 2: 7 14 1 8 7 2 4 11 15 Sample Output 2: No Solution 题解: 1.此题直接两个数两个数遍历会超时 2.设定一个cnt[]数组,下标表示数的大小,其值表示为该数出现的次数 3.i从0开始遍历,跳过cnt[i]==0的数,记t=m-i,若cnt[t] >=1 找到匹配,输出结果 代码: #i.原创 2020-09-02 11:26:26 · 2568 阅读 · 0 评论 -
[C++] PAT 1148 Werewolf - Simple Version (20分)
Sample Input 1: 5 -2 +3 -4 +5 +4 Sample Output 1: 1 4 Sample Input 2: 6 +6 +3 +1 -5 -2 +4 Sample Output 2 (the solution is not unique): 1 5 Sample Input 3: 5 -2 -3 -4 -5 -1 Sample Output 3: No Solution 题解 用v[]存储每个人说的话, 让i从1~n,j从i+1~n,表示第i和j个人是.原创 2020-08-07 18:30:13 · 2879 阅读 · 0 评论 -
[C++] PAT 1006 Sign In and Sign Out (25分)
Sample Input: 3 CS301111 15:30:28 17:00:10 SC3021234 08:00:00 11:25:25 CS301133 21:45:00 21:58:40 Sample Output: SC3021234 CS301133 题解: 将时间直接转化为数值 #include <iostream> #include <string> #include <vector> using namespace std; int in_.原创 2020-08-02 23:59:21 · 2562 阅读 · 0 评论 -
[C++] PAT 1005 Spell It Right (20分)
Sample Input: 12345 Sample Output: one five 题解: 输入超过了整型的范围,所以使用字符串进行操作,对字符串进行累加存入整型变量中,注意输入为0的情况 #include <iostream> #include <string> #include <vector> #include <cstring> #include <algorithm> using namespace std; string.原创 2020-08-02 23:46:51 · 2680 阅读 · 0 评论 -
[C++] PAT 1096 Consecutive Factors (20分)
Sample Input: 630 Sample Output: 3 567 题解: 此题从2开始循环,一直连乘, 如果连乘结果不是n的因子,跳出当前连乘,记录长度,和连乘开始的数字 注意素数的处理 #include <iostream> #include <cmath> using namespace std; int main(){ int n; cin >>n; int len = 0,first =0,maxn = sqrt(n)+1; for.原创 2020-07-19 22:17:56 · 2289 阅读 · 0 评论 -
[C++] PAT 1100 Mars Numbers (20分)
Sample Input: 4 29 5 elo nov tam Sample Output: hel mar may 115 13 题解: 用map将字符串与文字进行对应,视情况输出 #include <iostream> #include <string.h> #include <string> #include <unordered_map> using namespace std; string base_digit[13] = {"tre.原创 2020-07-19 21:40:35 · 2175 阅读 · 0 评论 -
[C++] PAT 1101 Quick Sort (25分)
Sample Input: 5 1 3 2 4 5 Sample Output: 3 1 4 5 题解: 直接先对数组排序, 如果排序后的元素i下标index在这里插入代码片保持不变,并且i大于原数组中i之前最大的元素max,则将i加入结果数组 #include <iostream> #include <vector> #include <algorithm> using namespace std; struct node{ int index; lon.原创 2020-07-19 18:53:02 · 1974 阅读 · 0 评论 -
[C++] PAT 1104 Sum of Number Segments (20分)
Sample Input: 4 0.1 0.2 0.3 0.4 Sample Output: 5.00 题解: 输入 0.1 0.2 0.3 0.4 取i从1开始, 对于输入的第i个数,比如i=2,包含第2个数的组合从其两侧考虑, 对于0.2,左边可以为0,可以为 0.1 ,有i==2个可能 其右边可以为 0.3、0.4、0,有n-i+1总可能 那么有i*(n-i+1)包含0.2的情况,让 ans += 0.2 * i * (n-i+1) 由于double对于大规模运算会有精度问题,.原创 2020-07-19 17:07:42 · 1969 阅读 · 0 评论 -
[C++] PAT 1108 Finding Average (20分)
Sample Input 1: 7 5 -3.2 aaa 9999 2.3.4 7.123 2.35 Sample Output 1: ERROR: aaa is not a legal number ERROR: 9999 is not a legal number ERROR: 2.3.4 is not a legal number ERROR: 7.123 is not a legal number The average of 3 numbers is 1.38 Sample Input .原创 2020-07-19 15:09:19 · 1987 阅读 · 0 评论 -
[C++] PAT 1110 Complete Binary Tree (25分)
Sample Input 1: 9 7 8 - - - - - - 0 1 2 3 4 5 - - - - Sample Output 1: YES 8 Sample Input 2: 8 - - 4 5 0 6 - - 2 3 - 7 - - - - Sample Output 2: NO 1 题解: 使用链表构建树结构,注意每个节点都有一个parent属性,最后根节是时直接判断parent为NULL的节点 #include <iostream> #include <s.原创 2020-07-19 13:12:58 · 1967 阅读 · 0 评论 -
[C++] PAT Stucked Keyboard (20分)
Sample Input: 3 caseee1__thiiis_iiisss_a_teeeeeest Sample Output: ei case1__this_isss_a_teest 题解: 用一个map<char,bool> 存储所有字符是不是坏键, 考虑到出现k=3 aabbbaaa这种a不是坏键的情况 用一个unordered_set<char>,存储所有不是坏键的字符 之后遍历字符串,设立一个cnt,每次遍历到字符i,接着遍历i后k个字符j, 如果j==i,cn.原创 2020-07-19 11:39:25 · 1969 阅读 · 0 评论 -
[C++] PAT 1117 Eddington Number (25分)
Sample Input: 10 6 7 6 9 3 10 8 2 7 8 Sample Output: 6 题解: 此题要找到的是骑乘距离大于E的天数E中最大的那个E,需要在所有数据输入之后进行处理,比如输入数据 天数 1 2 3 4 5 6 7 8 9 10 距离 6 7 6 9 3 10 8 2 7 8 第i天需要找出在所有天数中大于天数i的距离 先对距离进行降序排序 天数 1 2 3 4 5 6 7 8 9 10 距离 10 9 8 8 7 7 6 6 .原创 2020-07-18 19:04:24 · 2044 阅读 · 0 评论 -
[C++] PAT 1126 Eulerian Path (25分)
Sample Input 1: 7 12 5 7 1 2 1 3 2 3 2 4 3 4 5 2 7 6 6 3 4 5 6 4 5 6 Sample Output 1: 2 4 4 4 4 4 2 Eulerian Sample Input 2: 6 10 1 2 1 3 2 3 2 4 3 4 5 2 6 3 4 5 6 4 5 6 Sample Output 2: 2 4 4 4 3 3 Semi-Eulerian Sample Input 3: 5 8 1 2 2 5 5 4 4.原创 2020-07-18 16:38:42 · 1974 阅读 · 0 评论 -
[C++] PAT 1137 Final Grading (25分)
Sample Input: 6 6 7 01234 880 a1903 199 ydjh2 200 wehu8 300 dx86w 220 missing 400 ydhfu77 99 wehu8 55 ydjh2 98 dx86w 88 a1903 86 01234 39 ydhfu77 88 a1903 66 01234 58 wehu8 84 ydjh2 82 missing 99 dx86w 81 Sample Output: missing 400 -1 99 99 ydjh2 200 9.原创 2020-07-18 15:45:37 · 1944 阅读 · 0 评论 -
[C++] PAT 1141 PAT Ranking of Institutions (25分)
Sample Input: 10 A57908 85 Au B57908 54 LanX A37487 60 au T28374 67 CMU T32486 24 hypu A66734 92 cmu B76378 71 AU A47780 45 lanx A72809 100 pku A03274 45 hypu Sample Output: 5 1 cmu 192 2 1 au 192 3 3 pku 100 1 4 hypu 81 2 4 lanx 81 2 题解: 直接建立unordere.原创 2020-07-18 11:59:58 · 1978 阅读 · 0 评论 -
[C++] PAT 1144 The Missing Number (20分)
Sample Input: 10 5 -25 9 6 1 3 4 2 5 17 Sample Output: 7 题解: 直接用map对每一个输入数字计数,之后从i=1开始遍历map,如果map[i] == 0,直接输出,停止循环 在#include <iostream> #include <map> using namespace std; int n; map<int,int> mp; int main(){ cin >> n; .原创 2020-07-18 10:22:08 · 1969 阅读 · 0 评论 -
[C++] PAT 1152 Google Recruitment (20分)
Sample Input 1: 20 5 23654987725541023819 Sample Output 1: 49877 Sample Input 2: 10 3 2468024680 Sample Output 2: 404 题解: 直接枚举所有k位子字符串,判断其是不是素数,0和1不是素数,需特殊处理 #include <iostream> #include <string> using namespace std; int l,k; string .原创 2020-07-18 10:08:17 · 2375 阅读 · 0 评论 -
[C++] PAT 1154 Vertex Coloring (25分)
Sample Input: 10 11 8 7 6 8 4 5 8 4 8 1 1 2 1 4 9 8 9 1 1 0 2 4 4 0 1 0 1 4 1 0 1 3 0 0 1 0 1 4 1 0 1 0 0 8 1 0 1 4 1 0 5 3 0 1 2 3 4 5 6 7 8 8 9 Sample Output: 4-coloring No 6-coloring No 题解: 直接将所有边存储起来,另外用一个color[]存储每个节点的颜色,循环判断所有边的节点颜色是否相等 #include.原创 2020-07-17 10:57:37 · 1981 阅读 · 0 评论 -
[C++] PAT 1147 Heaps (30分)
Sample Input: 3 8 98 72 86 60 65 12 23 50 8 38 25 58 52 82 70 60 10 28 15 12 34 9 8 56 Sample Output: Max Heap 50 60 65 72 12 23 86 98 Min Heap 60 58 52 38 82 70 25 8 Not Heap 56 12 34 28 9 8 15 10 题解: 由于是完全二叉树,直接采用访问数组下标的方式解决该题 #include <iostream&.原创 2020-07-16 16:40:54 · 1938 阅读 · 0 评论 -
[C++] PAT 1151 LCA in a Binary Tree (30分)
Sample Input: 6 8 7 2 3 4 6 5 1 8 5 3 7 2 6 4 8 1 2 6 8 1 7 9 12 -3 0 8 99 99 Sample Output: LCA of 2 and 6 is 3. 8 is an ancestor of 1. ERROR: 9 is not found. ERROR: 12 and -3 are not found. ERROR: 0 is not found. ERROR: 99 and 99 are not found. 题解: .原创 2020-07-16 15:23:02 · 1964 阅读 · 0 评论 -
[C++] PAT 1143 Lowest Common Ancestor (30分)
Sample Input: 6 8 6 3 1 2 5 4 8 7 2 5 8 7 1 9 12 -3 0 8 99 99 Sample Output: LCA of 2 and 5 is 3. 8 is an ancestor of 7. ERROR: 9 is not found. ERROR: 12 and -3 are not found. ERROR: 0 is not found. ERROR: 99 and 99 are not found. 题解: 利用中map自动排序的特性, 对.原创 2020-07-16 11:49:09 · 1955 阅读 · 0 评论 -
[C++] PAT 1139 First Contact (30分)
Sample Input: 10 18 -2001 1001 -2002 -2001 1004 1001 -2004 -2001 -2003 1005 1005 -2001 1001 -2003 1002 1001 1002 -2004 -2004 1001 1003 -2002 -2003 1003 1004 -2002 -2001 -2003 1001 1003 1003 -2001 1002 -2001 -2002 -2003 5 1001 -2001 -2003 1001 1005 -2001 .原创 2020-07-16 10:35:34 · 2429 阅读 · 0 评论 -
[C++] PAT 1135 Is It A Red-Black Tree (30分)
Sample Input: 3 9 7 -2 1 5 -4 -11 8 14 -15 9 11 -2 1 -7 5 -4 8 14 -15 8 10 -7 5 -6 8 15 -11 17 Sample Output: Yes No No 题解: 本题考察红黑树,可以根据题目给出的数据直接构建二叉树,不需要去了解红黑树在构建过程中的左旋右旋以及其他规则, 根据红黑树的定义: 1.每个节点只能是红色或黑色 2.根节点是黑色 3.每个为NULL的叶子节点是黑色 4.如果节点为红色,他的左右孩子均为黑色.原创 2020-07-14 16:29:46 · 1982 阅读 · 0 评论 -
[C++] PAT ZigZagging on a Tree (30分)
Sample Input: 8 12 11 20 17 1 15 8 5 12 20 17 11 15 8 5 1 Sample Output: 1 11 5 8 17 12 20 15 题解: 1.先用中序-后序构建二叉树, 2.设立stack<node*> odd,表示将奇数层结点的孩子结点放入odd中,先放右孩子,再放左孩子 3.设立stack<node*> even,表示将偶数层结点的孩子结点放入even中,先放左孩子,再放右孩子 4.根节为第1层(奇数层),将ro.原创 2020-07-14 11:03:13 · 1966 阅读 · 0 评论 -
[C++] PAT 1123 Is It a Complete AVL Tree (30分)
Sample Input 1: 5 88 70 61 63 65 Sample Output 1: 70 63 88 61 65 YES Sample Input 2: 8 88 70 61 96 120 90 65 68 Sample Output 2: 88 65 96 61 70 90 120 68 NO 题解: 这里给出所有需要旋转的情况 判断是否是完全二叉树,可以依据树的下标结构 判断其输出的结点的下标是不是连续的,若是连续的下标,则该树是完全二叉树 #include <.原创 2020-07-14 00:38:55 · 1976 阅读 · 0 评论 -
[C++] PAT 1119 Pre- and Post-order Traversals (30分)
Sample Input 1: 7 1 2 3 4 6 7 5 2 6 7 4 5 3 1 Sample Output 1: Yes 2 1 6 4 7 3 5 Sample Input 2: 4 1 2 3 4 2 4 3 1 Sample Output 2: No 2 1 3 4 题解: 给出树的先序和后序遍历, 这里先把树结构给出: 可以发现在先序遍历中节点i的后一个节点必定是在其子树中,比如1的后一个节点2。同样,在后序遍历中节点i的前一个节点必定是在其子树中,比如1的前一个节.原创 2020-07-13 18:49:37 · 2024 阅读 · 0 评论 -
[C++] PAT 1115 Counting Nodes in a BST (30分)
Sample Input: 9 25 30 42 16 20 20 35 -5 28 Sample Output: 2 + 4 = 6 题解: 1.直接采用结构体构建一棵BST, 2.使用max_depth记录最大深度,使用num_depth[]记录每个深度有多少个节点, 3.输出最后两个深度节点数及其和 注意:如果在构建的时候就更新max_depth和num_depth[],无法通过全部测试点 #include <iostream> using namespace std; st.原创 2020-07-12 11:28:20 · 1986 阅读 · 0 评论 -
[C++] PAT 1107 Social Clusters (30分)
Sample Input: 8 3: 2 7 10 1: 4 2: 5 3 1: 4 1: 3 1: 4 4: 6 8 1 5 1: 4 Sample Output: 3 4 3 1 题解: 该题考察查并集, 1.建立一个course[],course[t]代表第一个选择课程t的人 2.建立father[], 3.进行输入,对于每个人i,若其选择的course[t]==0,则将其赋值给course[t],否则修改其father,使其与course[t]的father相等 4.再次遍历所有人,更新r.原创 2020-07-12 10:41:15 · 2043 阅读 · 0 评论 -
[C++] PAT Build A Binary Search Tree (30分)
Sample Input: 9 1 6 2 3 -1 -1 -1 4 5 -1 -1 -1 7 -1 -1 8 -1 -1 73 45 11 58 82 25 67 38 42 Sample Output: 58 25 82 11 38 67 45 73 42 题解: 在二叉搜索树中,对其进行中序遍历的结果是给出的数值序列从小到大的排列顺序,利用该该特性,以中序遍历方式构建二叉树的同时为节点赋值,最后再进行一次层次遍历即可 #include <iostream> #include &l.原创 2020-07-11 21:51:17 · 2003 阅读 · 0 评论 -
[C++] PAT 1091 Acute Stroke (30分)
Sample Input: 3 4 5 2 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 0 0 1 1 0 0 1 1 1 0 1 1 0 1 0 0 0 0 0 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 Sample Output: 26 题解: 此题直接采用BFS,对遍历过的点需要标记,避免再次遍历 #include <iostream> #include <queue> using name.原创 2020-07-09 15:46:32 · 1963 阅读 · 0 评论 -
[C++] PAT All Roads Lead to Rome (30分)
Sample Input: 6 7 HZH ROM 100 PKN 40 GDN 55 PRS 95 BLN 80 ROM GDN 1 BLN ROM 1 HZH PKN 1 PRS ROM 2 BLN HZH 2 PKN GDN 1 HZH PRS 1 Sample Output: 3 3 195 97 HZH->PRS->ROM 题解 Dijkstra+DFS,在DFS中判断的时候先判断happness之后是cost #include <iostream> #inclu.原创 2020-07-09 12:30:47 · 2174 阅读 · 0 评论 -
[C++] PAT 1080 Graduate Admission (30分)
Sample Input: 11 6 3 2 1 2 2 2 3 100 100 0 1 2 60 60 2 3 5 100 90 0 3 4 90 100 1 2 0 90 90 5 1 3 80 90 1 0 2 80 80 0 1 2 80 80 0 1 2 80 70 1 3 2 70 80 1 2 3 100 100 0 2 4 Sample Output: 0 10 3 5 6 7 2 8 1 4 题解 设立学生、学校结构体,学校结构体中设置last_grade和last_ge保存最后.原创 2020-07-09 11:00:02 · 1958 阅读 · 0 评论 -
[C++] PAT Gas Station (30分)
Sample Input 1: 4 3 11 5 1 2 2 1 4 2 1 G1 4 1 G2 3 2 3 2 2 G2 1 3 4 2 3 G3 2 4 G1 3 G2 G1 1 G3 G2 2 Sample Output 1: G1 2.0 3.3 Sample Input 2: 2 1 2 10 1 G1 9 2 G1 20 Sample Output 2: No Solution 题解 最短路径问题,使用二维数组存储图结构,将站点拼接到房屋节点之后,使用Dijkstra求每个站点.原创 2020-07-08 15:49:37 · 2036 阅读 · 0 评论 -
[C++] PAT 1064 Complete Binary Search Tree (30分)
Sample Input: 10 1 2 3 4 5 6 7 8 9 0 Sample Output: 6 3 8 1 5 7 9 0 2 4 题解: 在完全二叉搜索树中,其中序遍历必然是从小到大,可以直接对输入数据进行排序,直接得到其中序遍历; 再模拟一次中序遍历,将数组填充数字后就可以得到以数组方式存储的完成二叉搜索树; 直接输出数组即可得到其层次遍历 #include <iostream> #include <algorithm> using namespace s.原创 2020-07-07 11:26:02 · 1956 阅读 · 0 评论 -
[C++] PAT 1045 Favorite Color Stripe (30分)
Sample Input: 6 5 2 3 1 5 6 12 2 2 4 1 5 5 6 3 1 1 5 6 Sample Output: 7 题解: 该题运用动态规划,使用最长增长子序列(LIS)思想,在更新dp数组时需要判断当前颜色 i 和 i之前的颜色j,满足c_order[i] >= c_order[j]以及dp[j]+1 > dp[i],再更新dp[i] #include <iostream> using namespace std; int n,m,l; i.原创 2020-07-05 14:51:51 · 2027 阅读 · 0 评论 -
[C++] PAT 1038 Recover the Smallest Number (30分)
Sample Input: 5 32 321 3214 0229 87 Sample Output: 22932132143287 题解: 参考了大佬的代码 1.以字符串接收数据, 2.之后对字符串数组进行排序,采用了贪心策略, 3.最后将字符串数组进行拼接为字符串s,得到最小的数, 4.注意是以非0开始,若s.length() == 0直接输出0 #include <iostream> #include <string> #include <algorithm>.原创 2020-07-05 13:05:12 · 1965 阅读 · 0 评论 -
[C++] PAT 1022 Digital Library (30分)
Sample Input: 3 1111111 The Testing Book Yue Chen test code debug sort keywords ZUCS Print 2011 3333333 Another Testing Book Yue Chen test code sort keywords ZUCS Print2 2012 2222222 The Testing Book CYLL keywords debug book ZUCS Print2 2011 6 1: The Tes.原创 2020-07-04 13:21:51 · 2180 阅读 · 0 评论 -
[C++] PAT 1018 Public Bike Management (30分)
Sample Input: 10 3 3 5 6 7 0 0 1 1 0 2 1 0 3 3 1 3 1 2 3 1 Sample Output: 3 0->2->3 0 题解: 此题直接使用Djkstra+DFS,但要注意的是在经历每个点的时候就进行调整,而不是遍历完整个最短路径后进行调整,例: take表示从s0带出的自行车数量,re表示带回的自行车数量 1.从s0到s1时,take=2 2.从s1到s2时,re=3 3.从s2到s3时,re=7 4.从s3到s4时,发现s4需要.原创 2020-07-04 11:29:44 · 6255 阅读 · 0 评论 -
[C++] PAT 1004 Counting Leaves (30分)
Sample Input: 2 1 01 1 02 Sample Output: 0 1 题解: 给出一棵树,问每一层有多少个叶子节点,采用了DFS遍历 在#include <iostream> #include <vector> using namespace std; const int MAXN = 110; vector<int> T[MAXN];//T树结构 int n,depth[MAXN]={0};//n 节点数, depth[]深度 int .原创 2020-07-02 15:37:34 · 1997 阅读 · 0 评论 -
[C++] PAT 1057 Stack (30分)
Sample Input: 17 Pop PeekMedian Push 3 PeekMedian Push 2 PeekMedian Push 1 PeekMedian Pop Pop Push 5 Push 4 PeekMedian Pop Pop Pop Pop Sample Output: Invalid Invalid 3 2 2 1 2 4 4 5 3 Invalid 题解: 该题采用了分块思想 #include <iostream> #include <stack&.原创 2020-07-02 15:02:21 · 1955 阅读 · 0 评论