PAT(A)
花叶煦
不以物喜,不以己悲
展开
-
PAT(A) 1014 Waiting in Line (30 point(s)) C++ 题意理解
题意理解: 可看做八点前所有的顾客都已等在银行门外,排队依次进入 for those customers who cannot be served before 17:00, you must output Sorry instead. 17点以前可开始被服务的输出服务结束的时间,服务结束时间可能晚于17点;17点后才轮到的输出Sorry #include<cstdio> #include<vector> #include<climits> using namespa原创 2021-08-27 21:37:26 · 95 阅读 · 0 评论 -
PAT(A) 1020 Tree Traversals (25 point(s)) C++ 二叉树后序中序转层序
#include <cstdio> #include <vector> #include <map> using namespace std; int N; vector<int> post, in; map<int, int> level; void dfs(int root, int left, int right, int index){ if(left > right) return; level[index] =原创 2021-08-22 21:10:55 · 75 阅读 · 0 评论 -
PAT(A) 1019 General Palindromic Number (20 point(s))
#include <cstdio> using namespace std; int main(){ int N, b, i, flag = 1; int a[40]; scanf("%d %d", &N, &b); for(i = 0; N != 0; i++) a[i] = N % b, N /= b; for(int j = 0; j < i; j++) if(a[原创 2021-08-22 21:05:07 · 75 阅读 · 0 评论 -
PAT(A) 1017 Queueing at Bank (25 point(s)) C++ 测试点0,5
注意: 计算平均等待时间时应除以被银行服务的人数,而不是排队的总人数N,否则Case0:Wrong Answer 银行会服务完所有17点前开始排队的顾客,而不是到17点关门,否则Case5:Wrong Answer (Case5中最晚会服务到半夜零点半左右,这什么神仙银行! #include<cstdio> #include<vector> #include<algorithm> #include<cmath> using namespace std;原创 2021-08-22 20:58:28 · 252 阅读 · 0 评论 -
PAT(A) 1012 The Best Rank (25 point(s)) C++测试点2
注意: Case2 举例:分数:100,100,90,90,80 排名:1,1,3,3,5 对map的value排序:将map的key与value转为pair<key, value>存入vector,对vector使用sort排序. #include<cstdio> #include<vector> #include<map> #include<algorithm> using namespace std; struct node{原创 2021-08-22 10:27:11 · 132 阅读 · 0 评论 -
PAT(A) 1011 World Cup Betting (20 point(s))
#include <cstdio> int main(void){ float W, T, L, profit[3], sum; char choose[3]; for(int i = 0; i < 3; i++){ scanf("%f %f %f", &W, &T, &L); if(W > T && W > L) profit[i] = W, c原创 2021-08-21 22:13:59 · 54 阅读 · 0 评论 -
PAT(A) 1009 Product of Polynomials (25 point(s)) C++ 测试点0
注意:系数相加为零的要舍去,否则Case 0:Wrong Answer #include<cstdio> #include<map> using namespace std; int main(void){ int K, N; float a; map<int, float> p1, p2, m; scanf("%d", &K); for(int i = 0; i < K; i++){scanf("%d %原创 2021-08-21 21:33:57 · 157 阅读 · 0 评论 -
PAT(A) 1008 Elevator (20 point(s))
#include <cstdio> int main(void){ int N, floor = 0, next, sub, sum = 0; scanf("%d", &N); for(int i = 0; i < N; i++){ scanf("%d", &next); sub = next - floor; if(sub > 0) sum += 6 * sub; e原创 2021-08-21 21:14:44 · 63 阅读 · 0 评论 -
PAT(A) 1007 Maximum Subsequence Sum (25 point(s)) C++ 动态规划
动态规划 个人理解:temp是从i开始的每个data[i]的和,把temp看做序列第一个数,若temp是负数,则舍弃,从下一个数开始重新计算temp;若temp是正数,则采用,比较记录最大的sum #include <cstdio> int main(void){ int K, data[10001], start = -1, end, sum = -1; scanf("%d", &K); for(int i = 0; i < K; i++)原创 2021-08-21 21:12:44 · 73 阅读 · 0 评论 -
PAT(A) 1006 Sign In and Sign Out (25 point(s)) C++
int time = hh6060 + mm*60 +ss 记录时间,便于比较 #include<cstdio> #include<iostream> #include<string> using namespace std; struct node{ string id; int time; }; int main(void){ int M; scanf("%d", &M); node unlocked, l原创 2021-08-21 20:53:47 · 57 阅读 · 0 评论 -
PAT(A) 1005 Spell It Right (20 point(s)) C++
#include <iostream> #include <string> using namespace std; int main(void){ string s1, s2; cin >> s1; char digit[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"}; int sum = 0;原创 2021-08-21 20:42:05 · 68 阅读 · 0 评论 -
PAT(A) 1154 Vertex Coloring (25 point(s))
储存每条边,遍历每条边判断两个顶点的颜色是否相同 使用set计算颜色数 #include<bits/stdc++.h> using namespace std; int N, M, K; vector<pair<int, int>> v; vector<int> color; bool judge(void){ for(int i = 0; i < M; i++) if(color[v[i].first] == color[v原创 2021-08-20 11:11:14 · 61 阅读 · 0 评论 -
PAT(A) 1149 Dangerous Goods Packaging (25 point(s))
#include<bits/stdc++.h> using namespace std; vector<int> icp[100010]; bool judge(vector<int> v){ int n = v.size(); for(int i = 0; i < n; i++) for(int j = i+1; j < n; j++) if(find(icp[v[i]].begin(), icp[原创 2021-08-19 11:14:30 · 63 阅读 · 0 评论 -
PAT(A) 1147 Heaps (30 point(s))
#include<bits/stdc++.h> using namespace std; int M, N; vector<int> v; bool isMaxHeap(void){ for(int i = 0; i <= N/2; i++){ if(2*i+1 < N) if(v[2*i+1] > v[i]) return false; if(2*i+2 < N) i原创 2021-08-19 09:53:28 · 78 阅读 · 0 评论 -
PAT(A) 1144 The Missing Number (20 point(s))
#include<bits/stdc++.h> using namespace std; int main(void){ int N, a; set<int> s; scanf("%d", &N); for(int i = 0; i < N; i++){ scanf("%d", &a); if(a > 0) s.insert(a); } a = 1;原创 2021-08-16 11:54:01 · 51 阅读 · 0 评论 -
PAT(A) 1142 Maximal Clique (25 point(s))
#include<bits/stdc++.h> using namespace std; int Nv, Ne, edge[210][210]; bool isClique(vector<int> v){ int n = v.size(); for(int i = 0; i < n; i++) for(int j = i+1; j < n; j++) if(edge[v[i]][v[j]] == 0) retur原创 2021-08-15 18:57:44 · 66 阅读 · 0 评论 -
PAT(A) 1141 PAT Ranking of Institutions (25 point(s))
#include<bits/stdc++.h> using namespace std; struct node{ string sch; float tws = 0; int ns = 0; }; bool cmp(node a, node b){ if((int)a.tws != (int)b.tws) return a.tws > b.tws; if(a.ns != b.ns) return a.ns < b.ns; retu原创 2021-08-15 11:27:37 · 77 阅读 · 0 评论 -
PAT(A) 1140 Look-and-say Sequence (20 point(s))
#include<bits/stdc++.h> using namespace std; int main(void){ int D, N; scanf("%d %d", &D, &N); vector<int> va, vb; va.push_back(D); for(int i = 0; i < N-1; i++){ int p1 = 0, p2 = 0; while(p1原创 2021-08-15 10:58:50 · 502 阅读 · 0 评论 -
PAT(A) 1139 First Contact (30 point(s)) 测试点
测试点2:输入存在“-0000” 测试点3、4、5:AB ≠ CD #include<bits/stdc++.h> using namespace std; int N, M, K; string v1, v2; vector<int> gender; vector<vector<int>> v; map<string, int> m1; // m1: id->index map<int, string> m2; // m2.原创 2021-08-16 11:41:50 · 231 阅读 · 0 评论 -
PAT(A) 1138 Postorder Traversal (25 point(s))
#include<bits/stdc++.h> using namespace std; int N, post = -1; vector<int> pre, in; void dfs(int root, int beg, int en){ if(beg > en || post != -1) return; int i = beg; while(i <= en && pre[root] != in[i]) i++; d原创 2021-08-14 21:23:20 · 51 阅读 · 0 评论 -
PAT(A) 1137 Final Grading (25 point(s))
审题啊呜呜呜!final grade和Gfinal不是同一个东西!!!大哭.gif #include<bits/stdc++.h> using namespace std; struct node{ string id; int Gp, Gmid, Gfinal, G; }; bool cmp(node a, node b){ if(a.G == b.G) return a.id < b.id; return a.G > b.G;原创 2021-08-14 20:55:55 · 77 阅读 · 0 评论 -
PAT(A) 1136 A Delayed Palindrome (20 point(s))
#include<bits/stdc++.h> using namespace std; string add(string sa, string sb){ string sc = ""; int n = sa.size(), tag = 0; for(int i = n-1; i >= 0; i--){ char c = sa[i] + sb[i] - '0' + tag; if(c > '9') c -= 10, tag原创 2021-08-14 17:37:17 · 73 阅读 · 0 评论 -
PAT(A) 1135 Is It A Red-Black Tree (30 point(s))
性质:二叉搜索树的中序遍历序列为递增序列 前序+中序构建二叉搜索树 审题不够细致,(4)(5)性质都是for each node #include<bits/stdc++.h> using namespace std; struct node{ int left, right, value; }; int K, N; vector<node> v; vector<int> pre, in; set<int> black; bool cmp(i.原创 2021-08-14 09:43:42 · 74 阅读 · 0 评论 -
PAT(A) 1134 Vertex Cover (25 point(s))
#include<bits/stdc++.h> using namespace std; struct node{ int v1, v2; }; int main(void){ int N, M; scanf("%d %d", &N, &M); vector<node> edge(M); for(int i = 0; i < M; i++) scanf("%d %d", &edge[i原创 2021-08-13 20:22:26 · 52 阅读 · 0 评论 -
PAT(A) 1133 Splitting A Linked List (25 point(s))
#include<bits/stdc++.h> using namespace std; struct node{ int add, data, next; }; int main(void){ int addr, N, K; scanf("%d %d %d", &addr, &N, &K); vector<node> d(100010); for(int i = 0; i < N; i++){原创 2021-08-13 18:16:26 · 61 阅读 · 0 评论 -
PAT(A) 1132 Cut Integer (20 point(s))
#include<bits/stdc++.h> using namespace std; int main(void){ int N; scanf("%d", &N); for(int i = 0; i < N; i++){ long long Z, a, b, n; string s, sa, sb; scanf("%lld", &Z); s = to_string(Z);原创 2021-08-13 11:44:26 · 57 阅读 · 0 评论 -
PAT(A) 1130 Infix Expression (25 point(s))
#include<bits/stdc++.h> using namespace std; struct node{ string s; int left, right; }; int N, root = 1; vector<node> v; void dfs(int index){ if(index == -1) return; if((v[index].left != -1 || v[index].right != -1) &&原创 2021-08-13 11:16:52 · 58 阅读 · 0 评论 -
PAT(A) 1126 Eulerian Path (25 point(s))
#include<bits/stdc++.h> using namespace std; int N, M, cntCnn = 0, cntOdd = 0; vector<vector<int>> v; vector<int> cntDgr, vis; void dfs(int index){ vis[index] = 1; for(int i = 0; i < v[index].size(); i++) if(vis原创 2021-08-11 17:42:37 · 47 阅读 · 0 评论 -
PAT(A) 1125 Chain the Ropes (25 point(s))
贪心 注意:The result must be rounded to the nearest integer that is no greater than the maximum length. 即向下取整 见注释 #include<bits/stdc++.h> using namespace std; int main(void){ int N; scanf("%d", &N); vector<float> v(N); f.原创 2021-08-11 11:57:24 · 60 阅读 · 0 评论 -
PAT(A) 1121 Damn Single (25 point(s))
#include<bits/stdc++.h> using namespace std; int main(void){ int N, M, a, b, c[100005], g[100005] = {0}; set<int> s; fill(c, c+100005, 100000); scanf("%d", &N); for(int i = 0; i < N; i++){ scanf("%d %d",原创 2021-08-07 17:38:32 · 65 阅读 · 0 评论 -
PAT(A) 1120 Friend Numbers (20 point(s))
#include<bits/stdc++.h> using namespace std; int main(void){ int N; scanf("%d", &N); set<int> id; for(int i = 0; i < N; i++){ int sum = 0; string s; cin >> s; for(int j = 0; j &原创 2021-08-07 17:18:09 · 70 阅读 · 0 评论 -
PAT(A) 1117 Eddington Number (25 point(s))
#include<bits/stdc++.h> using namespace std; int main(void){ int N, E = 1, m[100005]; scanf("%d", &N); for(int i = 0; i < N; i++) scanf("%d", &m[i]); sort(m, m+N); for(int i = N-1; i >= 0; i--){原创 2021-08-07 12:15:48 · 231 阅读 · 0 评论 -
PAT(A) 1116 Come on! Let‘s C (20 point(s))
#include<bits/stdc++.h> using namespace std; bool isPrime(int x){ if(x == 2 || x == 3) return true; for(int i = 2; i*i <= x; i++) if(x % i == 0) return false; return true; } int main(void){ int N, K, i原创 2021-08-07 11:55:27 · 58 阅读 · 0 评论 -
PAT(A) 1115 Counting Nodes in a BST (30 point(s))
#include<bits/stdc++.h> using namespace std; struct node{ int left = -1, right = -1, value, level; }; int N, maxlevel = 0; vector<node> v; void buildBST(int i){ int j = 0, k, tag; while(j != -1){ k = j; if(v[i].va原创 2021-08-08 11:47:48 · 58 阅读 · 0 评论 -
PAT(A) 1113 Integer Set Partition (25 point(s))
#include<bits/stdc++.h> using namespace std; int main(void){ int N; scanf("%d", &N); vector<int> v(N); for(int i = 0; i < N; i++) scanf("%d", &v[i]); sort(v.begin(), v.end()); int n = N原创 2021-08-06 21:52:56 · 70 阅读 · 0 评论 -
PAT(A) 1112 Stucked Keyboard (20 point(s))
使用数组tag[128]记录键的好坏,假设所有的键都是坏的,tag[128]初始化为0,只要验证为是好键,对应tag[i] = 1 #include<bits/stdc++.h> using namespace std; bool cntSame(string s, int i, int k){ for(int j = 1; j < k; j++) if(s[i] != s[i+j]) return false; return tr原创 2021-08-06 21:35:52 · 66 阅读 · 0 评论 -
PAT(A) 1110 Complete Binary Tree (25 point(s))
Complete Binary Tree:完全二叉树 判断是否为完全二叉树:最后一个结点的位置序号是否为等于结点数 寻找根结点:输入中未出现的数字即为根结点 注意:N ≤ 20,序号不止一位数 #include<bits/stdc++.h> using namespace std; struct node{ int left = -1, right = -1; }; int N, root, maxpos = 0, last_node; vector<node> da.原创 2021-08-04 22:13:37 · 107 阅读 · 0 评论 -
PAT(A) 1100 Mars Numbers (20 point(s))
#include<bits/stdc++.h> using namespace std; int main(void){ string s1[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; string s2[13] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "he原创 2021-08-11 11:34:38 · 64 阅读 · 0 评论 -
PAT(A) 1099 Build A Binary Search Tree (30 point(s))
keys按升序排列,对已知结构BST填入值的过程即中序遍历 #include<bits/stdc++.h> using namespace std; struct node{ int left, right, value; }; int N, m = 0; vector<node> v; vector<int> key; void dfs(int index){ if(index == -1) return; dfs(v[index].lef原创 2021-08-12 22:22:21 · 60 阅读 · 0 评论 -
1087 All Roads Lead to Rome (30 point(s))
dijstra+dfs 使用map<string, int>以及map<int, string>对城市名和编号进行转换 注意:包括起点,ROM,共N个城市 #include<bits/stdc++.h> using namespace std; int N, K, maxHpp = 0, cntPath = 0, hpp[210], vis[210], d[210], cost[210][210]; double maxAveHpp = 0; map<stri.原创 2021-08-06 11:22:46 · 65 阅读 · 0 评论