PTA
文章平均质量分 70
Van0512
这个作者很懒,什么都没留下…
展开
-
09-排序3 Insertion or Heap Sort (25分)
这道题跟上一道很相似,不同的地方在于Heap Sort的处理。1. 找到下一个要和根节点交换(堆排序)的元素的下标。2. 利用“下滤”,做一次堆排序。#include #include using namespace std;bool isHeapSort(int *init, int *arr, int n);int getNextHeapSortIndex(int原创 2016-10-29 01:55:09 · 992 阅读 · 0 评论 -
08-图7 公路村村通 (30分)
邻接表 + Prim算法。#include #include #include using namespace std;struct Edge { Edge(const int &a, const int &b, const int &w): indexA(a), indexB(b), weight(w) {} int indexA; int原创 2016-10-31 05:41:58 · 1320 阅读 · 0 评论 -
02-线性结构3 Reversing Linked List (25分)
#include #include #include #include #include using namespace std;struct Node{ char addr[6]; int data; char next[6];};int main(void) { string firstAddr; int n, k; cin原创 2016-11-11 15:49:00 · 438 阅读 · 0 评论 -
01-复杂度2 Maximum Subsequence Sum (25分)
/* * 1. 最小的i和j * 2. 若序列全为0,输出头尾元素。*/#include using namespace std;int main(void) { bool isNegative(true); //记录序列是否全为负数 int first(0), last(0); //记录第一个和最后一个输入数据。 //当前和、最大和、最小i、最小j、当前原创 2016-11-11 14:46:12 · 493 阅读 · 0 评论 -
11-散列2 Hashing (25分)
分析:/* * quadratic probing 平方探测。 * 增量 * 正数增量,不考虑负数增量。*/代码:#include #include using namespace std;bool isPrime(const int &n) { if(n < 2) return false; if(n > 2 && n%2 == 0)原创 2016-11-11 04:58:56 · 743 阅读 · 0 评论 -
11-散列3 QQ帐户的申请与登陆 (25分)
使用STL,问题就变得简单了。#include #include #include using namespace std;int main(void) { int n; cin >> n; unordered_map myMap; myMap.reserve(n); char ch, id[20], pw[20]; for(int原创 2016-11-11 03:58:27 · 781 阅读 · 1 评论 -
11-散列1 电话聊天狂人 (25分)
第一种方法,简单粗暴的用unordered_map装填数据,再导入到vector中,再排序。使用unordered_map而不是map有几点好处:1. 效率更高。2. 有reserve函数,可以预备容量,控制内存不够的空间移动,进一步提高效率。这样的运行时间是209ms#include #include #include #include #include using原创 2016-11-11 02:23:47 · 2012 阅读 · 0 评论 -
10-排序6 Sort with Swap(0, i) (25分)
表排序的典型题目。分析:/* 假设每个环的元素个数分别为m1, m2, ... * 其中,单一元素的环说明了该元素在正确的位置上,不考虑。多元素的环,所有元素都不在正确的位置上。 * * 1. 第一个环(包括0的环),每次swap(0, i),i是arr[0]的值,都把i置于正确的位置。 * 最后一次同时把0和最后一个要交换的元素置于正确的位置,因此swap次数是原创 2016-11-10 23:08:43 · 1751 阅读 · 1 评论 -
09-排序2 Insert or Merge (25分)
在头文件 algorithm 中,还有像merge这样的函数。算法的选择是多种多样的,选择自己熟悉的。#include #include using namespace std;bool isMergeSort(int *init, int *arr, int n);int getMergeLength(int *arr, int n);void mergeSortOnce原创 2016-10-29 01:20:16 · 1487 阅读 · 0 评论 -
10-排序5 PAT Judge (25分)
排序规则:1. 总分相同,名次相同。 2. 总分相同,按照完全正确的题目数量排序。3. 1、2一致,根据id排名。4. 没有通过编译的,或者没有得分的,不列出。5. 保证至少有一个人能出现在排名表上。#include #include using namespace std;const int QUESTIONS = 6;const int MAXID = 1原创 2016-11-04 01:30:57 · 1606 阅读 · 1 评论 -
04-树5 Root of AVL Tree (25分)
/*平衡二叉树(Balanced Binary Tree)(AVL Tree)平衡因子(Balance Factor):BF(T) = LHeight - RHeight|BF| <= 1调整:1. 左子树的左边插入,LL插入:向右旋转(顺时针)。-> 为何叫做左单旋?唉,真是让人难以理解。2. 右子树的右边插入,RR插入:向左旋转(逆时针)。3. 左子树的右边插入,LR插入:对左原创 2016-10-31 21:54:12 · 470 阅读 · 0 评论