
PAT-Advanced
PAT 甲级
HHCCCaptain
这个作者很懒,什么都没留下…
展开
-
PAT 甲级 1123 Is It a Complete AVL Tree (30 分)
NoteAVL树构造 – 层序遍历 – 判断完全二叉树Code#include<bits/stdc++.h>using namespace std;struct node{ int data, height; node *left, *right;};node* newnode(int n) { node* root = new node; root->data = n; root->left = root->right = NULL; ret原创 2022-03-03 13:33:45 · 199 阅读 · 0 评论 -
PAT 甲级 1119 Pre- and Post-order Traversals (30 分)
Note根据前序+后序确定这棵二叉树,只有一个孩子的的情况被视作不唯一(孩子可左可右)。建树 – bfs确定unique – 中序遍历输出结果最后需要多输出一个换行符前两次备考都直接跳过这道题,这次居然一下AC了,巨开心!Code#include<bits/stdc++.h>using namespace std;bool flag = true; // uniquevector<int> pre, post, in;struct node{ int原创 2022-03-02 12:23:26 · 225 阅读 · 0 评论 -
PAT 甲级 1153 Decode Registration Card of PAT
Note排序细节繁多Code:#include<bits/stdc++.h>using namespace std;char lev;int n,m,sc,ty,te;unordered_map<char,int> lti;unordered_map<int,int> cnt,total,dti;struct node{ string id; char level; int site,date,testee,score;}stu[100原创 2021-09-09 16:01:43 · 133 阅读 · 0 评论 -
PAT 甲级 1121 Damn Single (25 分)
Note水题Code#include<bits/stdc++.h>using namespace std;int cp[100000];bool vis[100000]={false};int main(){ #ifndef ONLINE_JUDGE freopen("data.txt","r",stdin); #endif fill(cp,cp+100000,-1); int n,d1,d2,m; cin>>n; set<int>原创 2021-02-28 15:28:15 · 121 阅读 · 0 评论 -
PAT 甲级1120 Friend Numbers (20 分)
Note水题Code#include<bits/stdc++.h>using namespace std;int main(){ #ifndef ONLINE_JUDGE freopen("data.txt","r",stdin); #endif int n,data; cin>>n; set<int> s; for(int i=0;i<n;i++){ scanf("%d",&data); int temp=data原创 2021-02-28 15:26:42 · 113 阅读 · 0 评论 -
PAT 甲级 1117 Eddington Number (25 分)
Note水题Code1一开始for循环里用的是小于答案错误…改成小于等于 √#include<bits/stdc++.h>using namespace std;bool cmp(int a,int b){ return a>b;}int main(){ #ifndef ONLINE_JUDGE freopen("data.txt","r",stdin); #endif int n,edd; cin>>n; int num[n+1原创 2021-02-27 20:31:24 · 123 阅读 · 0 评论 -
PAT 甲级 1116 Come on! Let‘s C (20 分)
Note水题Code:#include<bits/stdc++.h>using namespace std;bool is_prime(int a){ if(a<=1) return false; for(int i=2;i<=sqrt(a);i++){ if(a%i==0) return false; } return true;}int num[10000];int main(){ #ifndef ONLINE_JUDGE freopen(原创 2021-02-27 20:29:01 · 122 阅读 · 0 评论 -
PAT 甲级 1113 Integer Set Partition (25 分)
Note水题Code:#include<bits/stdc++.h>using namespace std;int main(){ #ifndef ONLINE_JUDGE freopen("data.txt","r",stdin); #endif int n; cin>>n; int num[n]; for(int i=0;i<n;i++) scanf("%d",&num[i]); sort(num,num+n); int原创 2021-02-26 14:18:51 · 99 阅读 · 0 评论 -
PAT 甲级 1112 Stucked Keyboard (20 分)【测试点1】
Note细节!!!出现次数为K的倍数if(key[s[i]] == 0 && (cnt+1) % k == 0) key[s[i]] = -1;测试点1应该是类似:3 aaabbbcccaabb一开始判断会把a,b判断成Stucked Keyboard,但是后面又出现了不符合条件的相同字符,说明该字符应该不是Stucked Keyboard。此时应把之前的判断撤销。Code:#include<bits/stdc++.h>using namespa原创 2021-02-25 22:23:29 · 345 阅读 · 0 评论 -
PAT 甲级 1110 Complete Binary Tree (25 分)
Note完全二叉树Code:#include<bits/stdc++.h>using namespace std;int n,num[25];struct tree{ int left,right;}t[25];void create(int root,int i){ if(root>n) return ; num[i]=root; if(t[root].left!=-1) create(t[root].left,2*i); if(t[root].righ原创 2021-02-22 21:05:51 · 99 阅读 · 0 评论 -
PAT 甲级 1105 Spiral Matrix (25 分)
Note快乐模拟注意i代表行!!!j代表列!!!不要写反了(我可真行==Code:#include<bits/stdc++.h>using namespace std;int n;int findFac(){ vector<int> fac; int mini=1e8,best; for(int i=1;i<=sqrt(n);i++){ if(n%i==0) fac.push_back(i); } for(int i=fac.size()-1原创 2021-02-22 21:04:28 · 109 阅读 · 0 评论 -
PAT 甲级 1067 Sort with Swap(0, i) (25 分)【贪心】
Note贪心重在思路Code:#include<bits/stdc++.h>using namespace std;int main(){ #ifndef ONLINE_JUDGE freopen("data.txt","r",stdin); #endif int n,num,cnt=0; scanf("%d",&n); int index[n]; for(int i=0;i<n;i++){ scanf("%d",&num); i原创 2021-02-21 20:26:50 · 103 阅读 · 0 评论 -
PAT 甲级 1018 Public Bike Management (30 分)【Dijkstra+DFS】【坑·测试点5&7】
NoteDijkstra+DFS邻接矩阵注意看清题目要求 :先按send最小,若仍不唯一,按back最小。测试点5&7:只能沿着最短路径的方向收集多余自行车,分给后面的节点,后面节点多出来的不能填到前面去,只能计入回收总量。例如路径上自行车数为5->0->10,并不能把最后一个节点上挪5个给中间的,需要送出5个,并回收5个。所以总需求量不能用Cmax / 2 * 节点数 - 现有数来计算。Code#include<bits/stdc++.h>using原创 2021-02-20 20:34:10 · 446 阅读 · 0 评论 -
PAT 甲级 1087 All Roads Lead to Rome (30 分)【Dijkstra+DFS】
NoteDijkstra+DFS邻接矩阵模板题Code#include<bits/stdc++.h>using namespace std;const int INF=1e9;int n,k,cnt=2;int weight[205],graph[205][205],dist[205];vector<int> temp,ans;bool vis[205]={false};vector<int> pre[205];map<string,原创 2021-02-20 20:31:30 · 145 阅读 · 0 评论 -
PAT 甲级 1003 Emergency (25 分)【Dijkstra】
NoteDijkstra邻接表尽量用fill模板题Code#include<bits/stdc++.h>using namespace std;int n,team[510],dis[510];const int INF=1e9;struct Node{ int city,len; Node(int c,int l){ city=c; len=l; }};vector<Node> adj[510];int w[510],num[510];原创 2021-02-19 19:03:50 · 143 阅读 · 0 评论 -
PAT 甲级 1030 Travel Plan (30 分)【Dijkstra+DFS】
NoteDijkstra+DFS邻接矩阵模板题Code#include<bits/stdc++.h>using namespace std;int n,m,s,d,min_cost=1e9,totaldist;int dis[510];const int INF=1e9;struct Node{ int dist,cost; Node(int d,int c){ dist=d; cost=c; } Node(){}}adj[510][510];vec原创 2021-02-19 19:01:51 · 129 阅读 · 0 评论 -
PAT 甲级 1076 Forwards on Weibo (30 分)
Note图的遍历BFS用set会内存超限…Code#include<bits/stdc++.h>using namespace std;int L;vector<int> v[1010];//unordered_set<int> s;bool isvisit[1010]={false};struct node{ int root,level; node(int r,int l){ root=r; level=l; }};i原创 2021-02-18 19:48:45 · 124 阅读 · 0 评论 -
PAT 甲级 1034 Head of a Gang (30 分)
Note图的遍历DFS注意分辨边权与点权!!!本道题就是利用点权。数组不能只开到1010,会出现段错误…ans记得排序。按字母表顺序可以用:bool compare(string a, string b) { return a + b < b + a; }写for循环的时候把i打成了1…无语Code#include<bits/stdc++.h>using namespace std;struct node{ int id,weight; n原创 2021-02-18 19:47:03 · 105 阅读 · 0 评论 -
PAT 甲级 1013 Battle Over Cities (25 分)
Note图的遍历DFSCode#include<bits/stdc++.h>using namespace std;int n;vector<int> v[1010];bool isvisit[1010];void dfs(int u,int depth){ isvisit[u]=true; for(int i=0;i<v[u].size();i++){ int data=v[u][i]; if(isvisit[data]==false){原创 2021-02-17 17:39:45 · 97 阅读 · 0 评论 -
PAT 甲级 1021 Deepest Root (25 分)
Note图的遍历BFSbfs中可以尽量减少if-else,不必要的判断可以放到bfs之外用set可以避免排序 (set有序),而且保证不会重复。但是set的遍历只能用迭代器!Code#include<bits/stdc++.h>using namespace std;struct Node{ int root,height; Node(int r,int h){ root=r; height=h; }};int n;vector<int>原创 2021-02-17 17:38:24 · 94 阅读 · 0 评论 -
PAT 甲级 1098 Insertion or Heap Sort (25 分)
Note堆注意判断是否是插入排序的条件Code#include<bits/stdc++.h>using namespace std;int req[110];void down_adjust(int low,int high){ int i=low,j=2*i; while(j<=high){ if(j+1<=high&&req[j+1]>req[j]){ j++; } if(req[j]>req[i]){原创 2021-02-17 00:06:22 · 134 阅读 · 0 评论 -
PAT 甲级 1108 Finding Average (20 分)
Note字符串处理不能在main中改变字符串本身的值!!!(例如s.substr(1)提取负号)Code#include<bits/stdc++.h>using namespace std;bool is_legal(string s){ int cnt=0; for(int i=0;i<s.length();i++){ if(!isdigit(s[i])&&s[i]!='.'&&s[i]!='-') return false原创 2021-02-17 00:04:09 · 97 阅读 · 0 评论 -
PAT 甲级 1107 Social Clusters (30 分)
Note并查集模板题Code#include<bits/stdc++.h>using namespace std;int father[1010];int find_father(int a){ while(a!=father[a]) a=father[a]; return a;}void union_(int a,int b){ int fa=find_father(a); int fb=find_father(b); if(fa!=fb) father[原创 2021-02-15 19:47:17 · 101 阅读 · 0 评论 -
PAT 甲级 1104 Sum of Number Segments (20 分)
Note单纯用double会错误~学习!Code:#include<iostream>#include<algorithm>#include<string>using namespace std;int main(){ int n; double num; long long sum=0; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lf",&num); sum原创 2021-02-15 19:45:23 · 86 阅读 · 0 评论 -
PAT 甲级 1064 Complete Binary Search Tree (30 分)
Note二叉查找树利用中序遍历是有序(递增)序列完全二叉树用数组呈现(根结点下标从1开始)Code#include<bits/stdc++.h>using namespace std;int n,i=1;int num[1010],ans[1010];void inorder(int index){ if(index>n) return; inorder(2*index); ans[index]=num[i]; i++; inorder(2*index+原创 2021-02-14 21:29:50 · 109 阅读 · 0 评论 -
PAT 甲级 1070 Mooncake (25 分)
Note贪心库存量也有可能是小数!!!要用doubleCode:#include<bits/stdc++.h>using namespace std;struct mooncake{ double price,amount;};bool cmp(mooncake a,mooncake b){ return a.price>b.price;}int main(){ #ifndef ONLINE_JUDGE freopen("data.txt","r",s原创 2021-02-14 20:56:15 · 114 阅读 · 0 评论 -
PAT 甲级 1083 List Grades (25 分)
Note排序简单题Code:#include<bits/stdc++.h>using namespace std;struct people{ string name,id; int grade;};bool cmp(people a,people b){ return a.grade>b.grade;}int main(){ #ifndef ONLINE_JUDGE freopen("data.txt","r",stdin); #endif原创 2021-02-13 20:03:41 · 206 阅读 · 0 评论 -
PAT 甲级 1092 To Buy or Not to Buy (20 分)
Note散列简单题Code#include<bits/stdc++.h>using namespace std;int main(){ #ifndef ONLINE_JUDGE freopen("data.txt","r",stdin); #endif int hash[200]={0}; string s1,s2; cin>>s1>>s2; int len1=s1.size(),len2=s2.size(); int count_n原创 2021-02-13 20:01:47 · 111 阅读 · 0 评论 -
PAT 甲级 1004 Counting Leaves (30 分)
Note树的遍历 – BFS简单题Code#include<bits/stdc++.h>using namespace std;int max_level=-1,level[100];int counts[100]={0};vector<int> tree[100];void bfs(int root){ queue<int> q; q.push(root); level[root]=1; while(!q.empty()){ int原创 2021-02-11 11:43:08 · 109 阅读 · 0 评论 -
PAT 甲级 1053 Path of Equal Weight (30 分)
Note树的遍历 – DFS输入完孩子结点就排序~Code#include<bits/stdc++.h>using namespace std;int S,ind=0;vector<int> tmp,ans[1000];struct node{ int weight; vector<int> child;}tree[110];bool cmp(int a,int b){ return tree[a].weight>tree[b].原创 2021-02-11 11:41:18 · 101 阅读 · 0 评论 -
PAT 甲级 1094 The Largest Generation (25 分)
Note树的遍历 – BFS简单题Code#include<bits/stdc++.h>using namespace std;int counts[100]={0};struct Tree{ vector<int> node; int level;}tree[100];void bfs(int root){ queue<int> q; q.push(root); tree[root].level=1; while(!q.empty(原创 2021-02-10 23:45:23 · 114 阅读 · 0 评论 -
PAT 甲级 1106 Lowest Price in Supply Chain (25 分)
Note树的遍历 – BFS简单题Code#include<bits/stdc++.h>using namespace std;int n;double p,r;struct Tree{ vector<int> child; int level;}tree[100005];void bfs(int root){ int min_level=1e6,cnt=0; queue<int> q; q.push(root); tree[roo原创 2021-02-10 23:43:50 · 124 阅读 · 0 评论 -
PAT 甲级 1099 Build A Binary Search Tree (30 分)
Note二叉查找树利用中序遍历是有序(递增)序列Code#include<bits/stdc++.h>using namespace std;int num[110],ind=0;struct Node{ int data; int left,right;}tree[110];void inorder(int root){ if(root==-1) return ; inorder(tree[root].left); tree[root].data=num[i原创 2021-02-10 13:26:43 · 99 阅读 · 0 评论 -
PAT 甲级 1100 Mars Numbers (20 分)
NotemapCode#include<bits/stdc++.h>using namespace std;map<string,int> high,low;string gaowei[13]={"000","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};string diwei[13]={"tret","jan","feb","mar","ap原创 2021-02-10 13:23:19 · 116 阅读 · 0 评论 -
PAT 甲级 1090 Highest Price in Supply Chain (25 分)
Note树的遍历 – BFSthe number of retailers — 是个数不是序号!!!简单题,注意细节!!!(太菜了orz)Code#include<bits/stdc++.h>using namespace std;int n,cnt=0,max_level=-1;double p,r,max=0.0;struct Node{ vector<int> child; double price; int level;}nod[100005]原创 2021-02-09 20:40:01 · 110 阅读 · 0 评论 -
PAT 甲级 1079 Total Sales of Supply Chain (25 分)
Note树的遍历 – DFSans类型用double!!!注意第二层for循环中i与j是否同名!!!简单题,注意细节!!!(太菜了orz)Code#include<bits/stdc++.h>using namespace std;int n;double ans=0;double p,r;struct Node{ vector<int> child; int amount; double price;}nod[100005];void dfs原创 2021-02-09 20:37:46 · 109 阅读 · 0 评论 -
PAT 甲级 1101 Quick Sort (25 分)
Notemaxleft—左侧元素的最大值;minright[i]—记录在当前位置右侧(包括此位置本身)的最小值测试点"格式错误"——无论有无结果最后一行均要printf(“\n”);Code#include<bits/stdc++.h>using namespace std;int n;int ints[100005];int maxleft=-1;int minright[100005]; //开数组记录这个位置的右侧最小值!bool yes(int p,int原创 2021-02-08 19:54:59 · 105 阅读 · 0 评论 -
PAT 甲级 1102 Invert a Binary Tree (25 分)
Note二叉树一开始没有读懂题…(太菜了orz)input:编号(第几行)代表根节点与左右孩子连接反转二叉树:我直接把左孩子存为右孩子,右孩子存为左孩子…明确给出结点编号之间的关系–>用"静态二叉树"Code#include<bits/stdc++.h>using namespace std;vector<vector<int>> order(2);struct node{ int data,left,right;}nod[15];原创 2021-02-08 15:17:11 · 105 阅读 · 0 评论 -
PAT 甲级 1086 Tree Traversals Again (25 分)
Note二叉树模板题 思路不难先序遍历&中序遍历–>后序遍历Code#include<bits/stdc++.h>using namespace std;vector<int> pre,in,post;struct node{ int data; node* left; node* right;};node* create(int pL,int pR,int iL,int iR){ if(pL>pR||iL>iR) re原创 2021-02-08 15:11:29 · 114 阅读 · 0 评论 -
PAT 甲级 1020 Tree Traversals (25 分)
Note二叉树模板题 思路不难后序遍历&中序遍历–>层序遍历Code#include<bits/stdc++.h>using namespace std;int post[40],in[40];vector<int> v;struct node{ int data; node* left; node* right;};node* create(int pL,int pR,int iL,int iR){ if(pL>pR||i原创 2021-02-08 15:10:07 · 131 阅读 · 0 评论