
PAT
文章平均质量分 78
枫流仁武
这个作者很懒,什么都没留下…
展开
-
PAT 1127 ZigZagging on a Tree
本题树的层序遍历的一个变形.试着用队列来解决问题,但是效果并不好,后来通过别人的启发指导,发现可以通过深度优先搜索来解决问题.首先设置变量:vector<int> depthNums[30];//最多有三十个节点int maxDepth=0;这两个变量代表的含义为每一层对应的节点值,从左导游加入到vector中以及整个树的最大深度.给出整个代码段:#include...原创 2019-03-03 16:54:51 · 90 阅读 · 0 评论 -
PAT 1059 Prime Factors
直接上程序,注意要把N为1的情况排除掉.#include <iostream>#include <cmath>#include <map>using namespace std;bool isPrime(long long N){ auto a= (long long)sqrt(N); for(long long i=2;i<...原创 2019-03-07 17:05:36 · 69 阅读 · 0 评论 -
1142 Maximal Clique
#include <iostream>#include <unordered_map>#include <unordered_set>#include <vector>using namespace std;unordered_map<int,unordered_set<int>> graph;int Nv, N...原创 2019-04-03 20:44:43 · 122 阅读 · 0 评论 -
PAT 1083
#include <iostream>#include <cstring>#include <string>#include <vector>#include <algorithm>using namespace std;struct student{ string name; string id; ...原创 2019-04-05 12:20:11 · 161 阅读 · 0 评论 -
PAT 1151 LCA in a Binary Tree
这道题目仅仅建立一棵树是不够的.需要找到树中每个值对应的中序遍历的顺序下表.这样,每次把当前值的下表和给定的下表a与b的下表进行比较,得到不同的结果主要思想就是建立索引.给出代码:#include <iostream>#include<vector>#include<cstdio>#include<unordered_map>s...原创 2019-03-31 21:10:45 · 98 阅读 · 0 评论 -
PAT 1032 Sharing
先找到两条链的长度,之后...还是要注意输出格式,除了-1之外所有数值为5位。#include <iostream>#include <unordered_set>#include <unordered_map>using namespace std;struct Node{ char c; int next; No...原创 2019-04-01 11:53:40 · 117 阅读 · 0 评论 -
1070 Mooncake
贪心算法,每次找单价最高的商品存货也要double类型,否则有的测试点过不去...#include <iostream>#include <algorithm>using namespace std;struct MoonCakes{ double amounts; double prices; double p;};bool ...原创 2019-04-06 10:59:26 · 143 阅读 · 0 评论 -
1119 Pre- and Post-order Traversals
这道题方法很巧妙,看了别人的博客才有了思路,关键就在建树的过程中稍微有点改变#include <iostream>#include <vector>using namespace std;int preOrder[32];int postOrder[32];struct TreeNode{ int val; TreeNode *left;...原创 2019-04-06 10:59:57 · 111 阅读 · 0 评论 -
1094 The Largest Generation
#include <iostream>#include <vector>#include <map>using namespace std;vector<int> vectors[100];int maxHeight=0;map<int,int> nums;void dfs(int id,int height){ ...原创 2019-04-11 11:46:19 · 112 阅读 · 0 评论 -
PAT 1013 Battle Over Cities
不要用并查集,会超时的说...还是深度优先搜索好用...#include <iostream>#include <string>#include <cstring>#include <vector>bool graph[1001][1001];bool visited[1001];int N,M,K;using namespa...原创 2019-04-11 12:10:12 · 93 阅读 · 0 评论 -
PAT 1115 Counting Nodes in a BST
典型的深度优先搜索算法题目.在深度优先搜索过程中,记录下是否树的最大深度和每个深度上节点的个数就可以了.#include <iostream>#include <map>int MAX_DEPTH=0;using namespace std;struct TreeNode{ int val; TreeNode* left; Tree...原创 2019-04-02 13:11:22 · 175 阅读 · 0 评论 -
1086 Tree Traversals Again
本题的关键在于入栈序列为整个树的先序遍历,出栈序列为整个树的中序遍历序列.#include <iostream>#include <stack>using namespace std;int inorder[35];int preorder[35];int res[35];int j=0;struct TreeNode{ int val; ...原创 2019-04-02 17:05:47 · 101 阅读 · 0 评论 -
PAT 1074 Reversing Linked List
参考网上的博客的算法才做对的...注意几个关键思想:1. 建立一个数组,数组的元素为第i个地址,然后在数组上进行每k位交换.2.注意在while循环后要对最后一个节点数据进行添加3.打印时-1单独处理代码如下:#include <iostream>#include <unordered_map>#include <cstring>...原创 2019-04-09 21:34:40 · 172 阅读 · 0 评论 -
1125 Chain the Ropes
就是一个简单的贪心算法...先合并小的,然后合并大的#include <iostream>#include <algorithm>using namespace std;int array[10005];int main() { int N; cin>>N; for (int i = 0; i < N; ++i) ...原创 2019-04-23 22:12:30 · 91 阅读 · 0 评论 -
PAT 1020 Tree Traversals
#include <iostream>#include <map>#include <vector>/** * 给定一棵树的后序和中序遍历,给出其对应的层序遍历 */using namespace std;int postorder[35];int inorder[35];map<int,vector<int>> ma...原创 2019-05-15 17:26:01 · 105 阅读 · 0 评论 -
PAT 1025 PAT ranking
题目要求:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the rank...原创 2019-06-07 19:09:29 · 150 阅读 · 0 评论 -
PAT 1077 Kuchiguse
就是一道找末尾公共子串的问题还高估了这道题,本以为需要去除空格的影响....#include <iostream>#include <string>#include <cstring>#include <vector>#include <climits>using namespace std;int N;strin...原创 2019-06-07 20:16:54 · 120 阅读 · 0 评论 -
PAT 1063 Set Similarity
本以为有什么优秀算法能够不用集合就能解决这道问题,但是并没有找到...也没有看见别人不用集合就能解决这个问题.#include <iostream>#include <unordered_set>using namespace std;int main() { int N; cin>>N; unordered_set<...原创 2019-06-07 20:53:41 · 115 阅读 · 0 评论 -
PAT 1150 Travelling Salesman Problem
旅行商问题需要判断是否是一条路径,是否是一条简单回路.可能判断过程有些冗余...#include <iostream>#include<vector>#include <cstring>#include <climits>#include <unordered_set>using namespace std;in...原创 2019-06-13 20:42:48 · 88 阅读 · 0 评论 -
PAT 1044 Shopping in Mars
Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position ...原创 2019-06-08 16:34:30 · 118 阅读 · 0 评论 -
PAT 1079 Total Sales of Supply Chain
A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)-- everyone involved in moving a product from supplier to customer.Starting from one root supplier, everyone on...原创 2019-06-09 12:24:07 · 304 阅读 · 0 评论 -
PAT 1133 Splitting A Linked List
注意输出格式#include <iostream>#include <vector>#include <map>using namespace std;struct Node{ int val,next;};map<int,Node*> map1;vector<int> vectors[3];int main...原创 2019-06-20 16:43:43 · 73 阅读 · 0 评论 -
PAT 1133 Splitting A Linked List
注意输出时不能越界用向量来保存满足条件的地址#include <iostream>#include <vector>using namespace std;struct Node{ int val,next;};Node map1[100000];vector<int> vectors[3];/** * 尚未完成 */in...原创 2019-06-19 16:37:00 · 84 阅读 · 0 评论 -
PAT 1080 Graduate Admission
按照给定规则排好序,从头到尾进行录取,注意要同时录取排名相同的所有学生.每个学校录取的人数可能超过给定的上限.这种题需要静下心来慢慢做,不能浮躁#include <iostream>#include <vector>#include <algorithm>#include <map>#include <cstring>...原创 2019-06-10 16:59:42 · 110 阅读 · 0 评论 -
PAT 1045 Favorite Color Stripe
Eva is trying to make her own color stripe out of a given one. She would like to keep only her favorite colors in her favorite order by cutting off those unwanted pieces and sewing the remaining parts...原创 2019-06-10 17:55:37 · 110 阅读 · 0 评论 -
PAT 1052 Linked List Sorting
A linked list consists of a series of structures, which are not necessarily adjacent in memory. We assume that each structure contains an integerkeyand aNextpointer to the next structure. Now give...原创 2019-06-20 16:37:51 · 77 阅读 · 0 评论 -
PAT 1122 Hamiltonian Cycle
The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a "Hamiltonian cycle".In this problem, you are supposed to tell if a given cycle ...原创 2019-06-20 19:23:20 · 82 阅读 · 0 评论 -
PAT 1126 Eulerian Path
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 firs...原创 2019-06-21 11:26:03 · 84 阅读 · 0 评论 -
PAT 1003 Universal Travel Sites
第一次尝试做顶级的试题,虽说代码还有很多不成熟的地方但能够做出来还是挺高兴的.考察的是最大流问题#include <iostream>#include <string>#include <map>#include <vector>#include <stack>#include <climits>#inc...原创 2019-06-11 19:56:17 · 1550 阅读 · 0 评论 -
PAT LCA in a Binary Tree
这道题做过好多遍了,核心思想在于建立一个倒排索引,保存每个元素及其中序遍历对应的下标#include <iostream>#include <vector>#include <unordered_map>int preOrder[10005];int inOrder[10005];struct TreeNode{ int val; ...原创 2019-06-11 21:14:13 · 166 阅读 · 0 评论 -
PAT 1101 Quick Sort
没有必要用long long数据类型,int类型就够用了#include <iostream>#include <vector>#include <climits>#include <set>typedef long long LL;using namespace std;int N;bool judge(int index,co...原创 2019-06-17 18:17:16 · 83 阅读 · 0 评论 -
PAT 1084 Broken Keyboard
唯一需要注意的就是"_"可能不存在,不要跳过这个符号#include <iostream>#include <unordered_set>using namespace std;int main() { string s1,s2; cin>>s1; cin>>s2; unordered_set<cha...原创 2019-06-17 19:58:44 · 94 阅读 · 0 评论 -
PAT 1121 Damn Single
#include <iostream>#include <vector>#include <map>#include <set>using namespace std;int main() {// std::cout << "Hello, World!" << std::endl; int N; ...原创 2019-06-21 19:02:36 · 97 阅读 · 0 评论 -
PAT 1109 Group Photo
这题答的不好...#include <iostream>#include <algorithm>#include <vector>using namespace std;struct person{ string name; int height;};bool cmp(person* p1,person* p2){ if...原创 2019-06-25 18:01:32 · 127 阅读 · 0 评论 -
PAT 1140 Look-and-say Sequence
注意如何把整数转化为字符串#include <iostream>#include <map>#include <vector>using namespace std;string& constructStr(string& s){ char pre=s[0]; int count=1; vector<c...原创 2019-06-25 18:23:51 · 209 阅读 · 0 评论 -
PAT 1069 The Black Hole of Numbers
直接上代码:#include <iostream>#include <vector>#include <algorithm>using namespace std;int findNum(vector<int>& vector1, bool flag){ int res=0; if (flag) { ...原创 2019-06-25 18:37:46 · 81 阅读 · 0 评论 -
PAT 1071 Speech Patterns
注意判断是否为字母和数字的方式#include <iostream>#include <vector>#include<map>using namespace std;map<string,int > map1;void split(string& s){ int len=s.length(); string...原创 2019-06-25 20:50:59 · 164 阅读 · 0 评论 -
PAT 1049 Counting Ones
这道题我认为还是挺难的...#include<cstdio>#include<iostream>#include <cmath>using namespace std;typedef long long LL;LL find(LL N,int index){ LL a=LL (pow(10,index)); LL gaowei=N...原创 2019-06-22 16:23:48 · 101 阅读 · 0 评论 -
PAT 1107 Social Clusters
When register on a social network, you are always asked to specify your hobbies in order to find some potential friends with the same hobbies. Asocial clusteris a set of people who have some of thei...原创 2019-06-22 19:25:31 · 95 阅读 · 0 评论 -
PAT 1108 Finding Average
注意字符串和数字之间的相互转换#include <iostream>#include<string>#include <cmath>using namespace std;bool judge(string& string1){ int count=0; for (int i1=0;i1<string1.length(...原创 2019-07-01 19:24:08 · 116 阅读 · 0 评论