
PAT刷题
Lafitteee拉菲
这个作者很懒,什么都没留下…
展开
-
PAT A1143 Lowest Common Ancestor (30分)BST的最近共同祖先
一开始想到在BST中查找结点,记录查找路径,然后比较两个结点a和b的查找路径,路径中最后一个相同的结点就是最近共同祖先。于是写出了下面的代码,但是中间两个测试点运行超时。超时的原因估计是建树和查找的过程耗费时间。#include <iostream>#include <vector>using namespace std;const int maxn = 10...原创 2020-04-13 12:30:41 · 112 阅读 · 0 评论 -
PAT A1135 Is It A Red-Black Tree (30分)红黑树判断
维基百科:红黑树(英语:Red–black tree)是一种自平衡二叉查找树,是在计算机科学中用到的一种数据结构,典型的用途是实现关联数组。它在1972年由鲁道夫·贝尔发明,被称为"对称二叉B树",它现代的名字源于Leo J. Guibas和Robert Sedgewick于1978年写的一篇论文。红黑树的结构复杂,但它的操作有着良好的最坏情况运行时间,并且在实践中高效:它可以在O(log n)...原创 2020-03-31 10:44:12 · 216 阅读 · 0 评论 -
PAT A1130表达式树转换为中缀表达式
给出二叉树,输出中缀表达式,并用括号表示优先级用递归来解决。根据当前遍历结点的左右子树的存在与否可以分为四种情况,其中只有三种情况有效:左右子树都存在:返回"(" + 递归访问左子树 + 该结点的值 + 递归访问右子树 + “)”;左空右不空:说明该结点是负号。返回"(" + 该结点的值 + 递归访问右子树 + “)”;左不空右空:表达式树中不存在这种情况左右全空:说明到达叶结点,...原创 2020-03-30 11:50:54 · 667 阅读 · 0 评论 -
PAT A1073 Scientific Notation (20分)科学计数法转成普通数字(恶心)
#include <iostream>#include <string>using namespace std;int main(){ string str; cin >> str; if(str[0] == '-') cout << '-'; //该数字为负数,先输出负号 int pos = 0; //记录'E'的下标 wh...原创 2020-03-22 18:37:20 · 114 阅读 · 0 评论 -
PAT A1105 Spiral Matrix (25分)快乐模拟
#include <iostream>#include <cmath>#include <algorithm>using namespace std;const int maxn = 10001;int matrix[maxn][maxn];int a[maxn], m, n, K;void find_m_n(){ //找到符合条件的m和n ...原创 2020-03-20 12:08:26 · 118 阅读 · 0 评论 -
PAT A1087 All Roads Lead to Rome (标尺较多的复杂Dijkstra + DFS)
总结:这种题目一看就是Dijkstra + DFS找最短路径的最优解,其复杂之处就在于给出了第二标尺、第三标尺甚至第四标尺,查找最优解的过程比较麻烦。解决办法如下:用vector<int> path[maxn]将所有的最短路径path都记录下来开一个结构体,成员分别为每一个ans的第二标尺计算结果、第三标尺计算结果…并记录下来对应的path编号对ans[]数组按要求sort,...原创 2020-03-12 22:11:20 · 219 阅读 · 0 评论 -
A1018 两个测试点不通过,这是为什么呢??(已解决)
#include <iostream>#include <cstring>#include <vector>using namespace std;const int maxn = 501;const int INF = 1000000000;int Cmax, N, Sp, M;int d[maxn], G[maxn][maxn];int w...原创 2020-03-12 12:45:41 · 358 阅读 · 0 评论 -
PAT A1064 完全二叉搜索树 与A1099思路相同
题目将完全二叉树与二叉搜索树结合起来,给定一个序列,要求输出其完全二叉搜索树的层序遍历。思路很重要:完全二叉树使用一个数组来存储对于一棵BST来说,其中序遍历序列是递增的所以,先将给出的原始序列进行递增排序然后对表示完全二叉树的数组进行中序遍历,在遍历过程中填入排序后的原始序列最后数组中存放的就是一棵完全二叉搜索树,存放顺序就是层序遍历的顺序。#include <iostr...原创 2020-03-08 11:12:35 · 177 阅读 · 0 评论 -
PAT A1022 Digital Library (30分)
之前未解决的一道题,这次按自己的思路一点点写出来的~#include <iostream>#include <string>#include <vector>#include <set>using namespace std;const int maxn = 10001;struct bookInfo{ string id, yea...原创 2020-03-02 15:31:02 · 109 阅读 · 0 评论 -
A1078平方探测法实现hash
实现平方探测法。注意代码中平方探测法探测过程中的易错点#include <iostream>#include <cmath>using namespace std;const int maxn = 10001;int hashTable[maxn] = {};int ans[maxn];bool isPrime(int x){ if(x <= 1)...原创 2020-02-29 11:39:45 · 205 阅读 · 0 评论 -
A1089(插入排序与归并排序过程中的判断)
此题的亮点在于判断是否是插入排序或者是归并排序后要输出下一趟排序的序列。锻炼插入排序与归并排序的代码#include <iostream>#include <algorithm>using namespace std;int ori[101], temp[101], target[101], n;bool same(int a[]){ for(int i ...原创 2020-02-26 16:58:48 · 257 阅读 · 0 评论 -
A1067(又是一道贪心算法题)
#include <iostream>#include <algorithm>using namespace std;const int maxn = 100001;int pos[maxn]; //记录数字所在的位置,数组下标为数字,值为所在的位置int main(){ int n, num; cin >> n; int left = n -...原创 2020-02-24 10:53:08 · 248 阅读 · 0 评论 -
A1033 To Fill or Not to Fill (25分)(贪心算法的难题)
这道题是个人觉得贪心算法里面比较复杂的了。值得反复回看。With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way fr...原创 2020-02-23 22:12:10 · 199 阅读 · 0 评论 -
PAT B1033 坏键盘打字 (2号测试点坏键为空,读入空字符串问题)
自己写的代码提交结果中间有一个测试点不通过,得到了19分:#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int maxn = 100001;bool valid[256];...原创 2020-02-17 18:29:47 · 373 阅读 · 0 评论 -
PAT B1029 旧键盘
#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <iostream>#include <cstring>using namespace std;int main() { char a[81], b[81]; int res[130] = {}; //数组下标为ASCII码...原创 2020-02-16 16:54:57 · 145 阅读 · 0 评论 -
PAT B1027 打印沙漏 (两个测试点不能通过)
#include <iostream>using namespace std;int func(int x) { //计算x层的沙漏需要多少个符号,返回符号的个数 int sum = 0; while (x >= 3) { sum += x; x -= 2; } return sum * 2 + 1;}int main() { int n; cha...原创 2020-02-16 16:10:54 · 587 阅读 · 4 评论