
PAT
主要是PAT甲级,能写出自己的写法就贴自己的写法,写不出的话也不会完全照抄题解。
KonoHT
这个作者很懒,什么都没留下…
展开
-
【天梯赛L2 栈混洗】L2-032 彩虹瓶 (25 分)
#include<bits/stdc++.h>using namespace std;int N, M, K;/*有一个有序序列A = {1, 2, 3, ..., n}判断序列B是否是有序序列A的栈混洗*/int main() { cin >> N >> M >> K; // 每次输入的乱序序列B都能通过一个中间栈进行临时存放,在某个时间出栈可以达到有序序列 for(int i = 0;i < K;++i)原创 2021-03-27 17:29:54 · 179 阅读 · 0 评论 -
【PAT甲级 水题】1036 Boys vs Girls (25 分)
#include<bits/stdc++.h>using namespace std;struct Stu { string name, gender, ID; int grade;};int main() { int N; cin >> N; Stu maxF = {"", "F", "", -1}; Stu minM = {"", "M", "", 101}; for(int i = 0;i < N;++i) {原创 2021-03-24 19:18:58 · 142 阅读 · 0 评论 -
【PAT甲级 水题】1035 Password (20 分)
#include<bits/stdc++.h>using namespace std;map<char, char> M = { {'1', '@'}, {'0', '%'}, {'l', 'L'}, {'O', 'o'}};int main() { int N; queue< pair<string, string> > rst; cin >> N; for(int i原创 2021-03-24 18:55:05 · 148 阅读 · 0 评论 -
【PAT甲级 映射】1022 Digital Library (30 分)
#include<bits/stdc++.h>using namespace std;int N, M;map<string, set<int>> Title, Author, Key, Puber, Year;void Query(map<string, set<int>> &mp, string & str) { // 根据信息在mp中查找对应id if(!mp[str].empty()) f原创 2021-03-23 23:09:00 · 153 阅读 · 2 评论 -
【PAT甲级 模拟 测试点0、3、4、5、7、8分析】1026 Table Tennis (30 分)
测试点分析都在代码注释里了K个球桌(编号1~K),但球桌都满的时候,有一条等待队伍有VIP的存在,所以在输入的最后给了VIP球桌的编号,当有VIP球桌开放的时候,队伍里面的第一个VIP将会无视他在队伍的顺序,直接进入该VIP球桌;如果没有VIP的话或VIP球桌那就按正常形式。营业时间为:8点~21点统计队伍中每个人的等待时间 和 每个球桌在这一天共招待了几对球友输出每对球友的到达时间 开始打球的时间。原创 2021-03-22 23:09:36 · 819 阅读 · 7 评论 -
【PAT甲级 模拟、优先队列、模拟时间】1017 Queueing at Bank (25 分)
#include<bits/stdc++.h>using namespace std;int N ,K;struct Cust{ int arriveTime; int processTime; Cust():arriveTime(0), processTime(0) {} bool operator<(const Cust &ob)const { // 优先队列的重载小于号是常引用并且是const函数 return ar原创 2021-03-18 14:26:49 · 209 阅读 · 0 评论 -
PAT2020春季甲级模拟
第一题AC#include<bits/stdc++.h>using namespace std;string Date;bool isPrime(int n) { int sq = sqrt(n); if(n <= 1) return false; for(int i = 2;i <= sq;++i) if(n % i == 0) return false; return true;}in.原创 2021-03-11 17:46:31 · 230 阅读 · 0 评论 -
【PAT甲级 素数判断】1152 Google Recruitment (20 分)
#include<bits/stdc++.h>using namespace std;int N, K;string num;bool isPrime(long long n) { int sq = sqrt(n); for(int i = 2;i <= sq;++i) if(n % i == 0) return false; return true;}int main() { cin >>原创 2021-03-10 15:20:20 · 188 阅读 · 0 评论 -
【PAT甲级 模拟】1016 Phone Bills (25 分)
算是学到了模拟时间了while(start.day < end.day || start.hour < end.hour || start.minute < end.minute){ // 每分钟做的事 // 模拟时间 start.minute++; if(temp.time.minute >= 60){ start.minute = 0; start.hour++; } if(temp.ti原创 2021-03-09 12:02:54 · 166 阅读 · 0 评论 -
【PAT甲级 图形输出】1031 Hello World for U (20 分)
#include<bits/stdc++.h>using namespace std;int N;string S;int main() { cin >> S; int N = S.size(); int n1, n2, n3; // 画图发现了规律…… if( (N-1) % 3 == 0){ n1 = n2 = n3 = (N+2)/3; } else if( (N-1) % 3 == 1 ){原创 2021-03-08 21:49:48 · 107 阅读 · 0 评论 -
【PAT甲级 进制转换】1027 Colors in Mars (20 分)
给定三个十进制数字,转13进制#include<bits/stdc++.h>using namespace std;map<int, char> M = { {0, '0'}, {1, '1'}, {2, '2'}, {3, '3'}, {4, '4'}, {5, '5'}, {6, '6'}, {7, '7'}, {8, '8'}, {9, '9'}, {10, 'A'}, {11, 'B'}, {12, 'C'}};string convert(in原创 2021-03-08 20:48:54 · 123 阅读 · 0 评论 -
【PAT甲级 大整数加法】1024 Palindromic Number (25 分)
#include<bits/stdc++.h>using namespace std;bool isPalindeomic(string S) { // 判断回文数 for(int i = 0;i < S.size()/2;++i) if(S[i] != S[S.size() - 1 - i]) return false; return true;}string add(string a, string b) { // 大整数加法 string rst; in原创 2021-03-07 11:07:28 · 152 阅读 · 0 评论 -
【PAT甲级 进制转换/回文数判断】1019 General Palindromic Number (20 分)
#include<bits/stdc++.h>using namespace std;vector<int> convertNum;void convert(long long num, int radix) { do { convertNum.push_back(num % radix); num /= radix; } while(num != 0);}int main() { long long num; int radix; cin >原创 2021-03-07 08:49:12 · 172 阅读 · 0 评论 -
【PAT甲级 图的遍历/ 树】1021 Deepest Root (25 分)
#include<bits/stdc++.h>using namespace std;bool visit[10010] = {false};vector<int> G[10010];int N;int Depths[10010] = {0};int compomentNum = 0;void DFS(int u, int depth) { visit[u] = true; Depths[u] = max(Depths[u], depth);原创 2021-03-05 22:44:28 · 127 阅读 · 0 评论 -
【PAT 甲级 简单哈希】1092 To Buy or Not to Buy (20 分)
#include<bits/stdc++.h>using namespace std;map<char, int> Cnt;int main() { string origin, target; cin >> origin >> target; for(char ch: origin) Cnt[ch]++; for(char ch: target) Cnt[ch]--; int原创 2021-03-05 21:46:41 · 156 阅读 · 0 评论 -
【PAT甲级 字符串】1073 Scientific Notation (20 分)
#include<bits/stdc++.h>using namespace std;string S;string num;int main() { cin >> S; if(S[0] == '-') cout << "-"; num.insert(num.begin(), S[1]); // 寻找字母E的位置,并把数字部分存储在num中 int i; for(i = 3;S[i] != 'E';++i)原创 2021-03-05 21:13:33 · 122 阅读 · 0 评论 -
【PAT 甲级 模拟】1014 Waiting in Line (30 分)
# include <bits/stdc++.h>using namespace std;/*N个窗口,黄线内才会有队伍(N条队伍),每条队伍最多有M个人;当黄线内的人没满(人数共小于NM个人),就可以直接得出答案了当黄线内的人满了(大于等于NM个人), 那么黄线外的人就需要等待窗口的人办完业务,然后等待某条队伍有人办理完业务 (如果有多条同时办理完业务的人而空出来的队伍,就选择编号较小的队伍) 银行营业时间为8~17点输出每位查询的顾客办理完业务的时间根据编号大小决定顺序原创 2021-03-05 18:39:47 · 164 阅读 · 0 评论 -
【PAT甲级 寻找最长回文串】1040 Longest Symmetric String (25 分)
我还以为是什么动态规划,做了一下就是简单的回文字符串的判断而已,从当前i处的字符左右同时遍历寻找相同字符,速度很快# include <bits/stdc++.h>using namespace std;string S;int main() { getline(cin, S); int maxLen = 1; for(int i = 0;i < S.size();++i){ int l = i-1, r = i+1;原创 2021-03-04 16:05:59 · 148 阅读 · 1 评论 -
【PAT甲级 哈希映射】1047 Student List for Course (25 分)
最后一个测试点数据太多了!!不能用cout和cin和string,要用char数组……可以用范围for语句#include<bits/stdc++.h>using namespace std;int N, K;char stuNum_stuName[40010][5] ;vector<int> course_stuNums[2510];bool cmp(int a, int b){ return strcmp(stuNum_stuName[a], stuNu原创 2021-03-03 22:11:49 · 158 阅读 · 1 评论 -
【PAT甲级 哈希映射/字符串哈希】1039 Course List for Student (25 分)
最后一个测试点数据量很大,用unordered_map和scanf速度会更快一点#include<bits/stdc++.h>using namespace std;int N, K;unordered_map<string, vector<int> > stu_course;int main() { cin >> N >> K; for(int i = 0;i < K;++i){ int cou原创 2021-03-03 21:26:32 · 186 阅读 · 1 评论 -
【PAT甲级 链表处理】1074 Reversing Linked List (25 分)
乙级也有这题,以前也写过,以前的写法更简单……【PAT乙级、C++】1025 反转链表 (25分)这次用了双指针和栈……#include<bits/stdc++.h>using namespace std;struct Node{ int data; int next;}node[100010];int start, N, K; int main() { cin >> start >> N >> K; fo原创 2021-03-03 20:55:18 · 160 阅读 · 1 评论 -
【PAT甲级 结构体排序水题】1083 List Grades (25 分)
# include <bits/stdc++.h>using namespace std;struct Stu{ string name, id; int grade;};int N, low, high;vector<Stu> stu;int main() { cin >> N; stu.resize(N); for(int i = 0;i < N;++i) cin >> stu[原创 2021-03-03 17:36:43 · 128 阅读 · 1 评论 -
【PAT甲级 链表处理】1133 Splitting A Linked List (25 分)
链表转换为线性表太方便了# include <bits/stdc++.h>using namespace std;struct Node{ int data; int next;}node[100010];vector<int> V,V_0toK, V_afterK;int start, N, K;int main() { cin >> start >> N >> K; for(int i =原创 2021-03-03 17:09:49 · 126 阅读 · 1 评论 -
【PAT甲级 结构体排序水题】1028 List Sorting (25 分)
为什么会有这么简单的题…………#include<bits/stdc++.h>using namespace std;struct Stu{ int id; string name; int score;};vector<Stu> stu;int N, C;int main() { cin >> N >> C; stu.resize(N); for(int i = 0;i < N;++i)原创 2021-03-03 16:30:41 · 148 阅读 · 1 评论 -
【PAT甲级 链表处理】1097 Deduplication on a Linked List (25 分)
更改链表结构# include<bits/stdc++.h>using namespace std;struct Node{ int data; int next;}node[100010];int start, N;bool visit[100010];int main() { cin >> start >> N; for(int i = 0;i < N;++i){ int now;原创 2021-03-03 16:06:06 · 137 阅读 · 2 评论 -
【PAT甲级 堆排序/折半插入排序】1098 Insertion or Heap Sort (25 分)
# include <bits/stdc++.h>using namespace std;vector<int> A;vector<int> A_origin;vector<int> seq;int N;bool isInsertSort = false;bool isHeapSort = false;bool isSameArray() { for(int i = 1;i <= N;++i) if(A[i]原创 2021-03-02 13:07:37 · 152 阅读 · 0 评论 -
【PAT甲级 堆的判断/树的遍历 C++】1155 Heap Paths (30 分)
DFS 1# include <bits/stdc++.h>using namespace std;vector<int> T;int N;vector<int> path;void DFS(int root) { if(root > N) return; if(root * 2 > N) { // 递归边界------叶子结点 path.push_back(root); for(int i =原创 2021-03-02 09:24:24 · 145 阅读 · 0 评论 -
【PAT甲级 堆/堆的判断/树的遍历 C++】1147 Heaps (30 分)
# include <bits/stdc++.h>using namespace std;int M, N;vector<int> A(1001); // 使用DFS遍历树,每次都只判断当前结点u和它的左后孩子符不符合堆的要求void DFS(int u, int &flag){ if(u == 1){ // 初始化第一次 if(N == 2) { if(A[u] >= A[u*2]) flag = 2;原创 2021-02-28 21:44:51 · 157 阅读 · 0 评论 -
1107 Social Clusters (30 分) 【PAT甲级 非传统并查集 C++写法】
不用传统的并查集用CLuster的map来创建 [集合编号:集合的映射] ,用每一个人的编号来作为集合的编号,也就是Cluster中的键(第一个进入集合的人的编号就是集合的编号(也可以理解为并查集中的根节点));用id数组来记录每一个人所在的集合的编号(也可以理解为并查集中的根节点)# include <bits/stdc++.h>using namespace std;int N;vector<int> hobby[1010];vector<int>原创 2021-02-23 17:45:33 · 175 阅读 · 0 评论 -
【PAT甲级 完全二叉树/二叉排序树】1064 Complete Binary Search Tree (30 分)
二叉排序树有一个性质:中序遍历得到的序列是有序序列所以将题目给定的序列排好序,然后将一个所有结点值为空的完全二叉排序树进行中序遍历(该完全二叉树还未建立,但是数组是存在的,所以每一个结点都是存在的),把中序遍历中每一个结点的值按照排序好的有序序列的顺序一一赋值,这样一颗完全二叉树就建立好了!而对完全二叉树数组进行顺序遍历就是对完全二叉树进行中序遍历# include <bits/stdc++.h>using namespace std;/* 二叉排序树有一个性质:中序遍历得到的原创 2021-02-22 16:42:19 · 134 阅读 · 0 评论 -
【PAT甲级 树/图的遍历】1106 Lowest Price in Supply Chain (25 分)
DFS# include <bits/stdc++.h>using namespace std;int N;double P, r;vector< vector<int> > G;double lowestPrice = 0xffffff;int cnt = 0;void DFS(int u, int lv){ if(G[u].size() == 0) { double price = P * pow((1 + 0.01*r),原创 2021-02-21 16:49:49 · 146 阅读 · 0 评论 -
【PAT甲级 树\图的遍历】1094 The Largest Generation (25 分)
# include <bits/stdc++.h>using namespace std;int N, M; // 结点数、有孩子的结点数vector<int> G[101];// 邻接表int level[101]; // 每一个结点所在的层数map<int, int> cnt; // 数每一层有多少个结点void DFS(int u, int lv){ level[u] = lv; if(G[u].size()原创 2021-02-21 15:34:24 · 129 阅读 · 0 评论 -
【PAT甲级 树的遍历 测试点6超时解决】1090 Highest Price in Supply Chain (25 分)
从叶子结点向根节点遍历:最后一个测试点超时最后一个测试点超时原因# include <bits/stdc++.h>using namespace std;int Root;int N;double P, r;vector<int> leafLevel;vector<int> father;vector<bool> isFather; // 不是父节点的结点是叶子结点int main(){ cin >> N >原创 2021-02-21 12:27:27 · 236 阅读 · 0 评论 -
【PAT甲级 树/图的遍历】1079 Total Sales of Supply Chain (25 分)
BFS# include <bits/stdc++.h>using namespace std;int N;double P, r; vector< vector<int> > G;map<int, int> leafProductAmount; // 这里面的键都是叶子结点,值都是叶子结点的product 的数量vector<int> level;// 广度优先遍历求路径长度(或者树的层数)void BFS(){原创 2021-02-21 10:53:42 · 144 阅读 · 0 评论 -
【PAT甲级 树的遍历 C++】1102 Invert a Binary Tree (25 分)
好的,我AC了,我比Max Howell强(狗头)在建树的时候就翻转建立,很简单就将两个指针域反过来就可以了,简单的不敢相信,Max Howell就这?(狗头保命!!!!!!!滑稽)# include <bits/stdc++.h>using namespace std;int N;int cnt;struct Node{ int l, r;}node[10];void levelOrder(int root){ queue<int> Q;原创 2021-02-20 21:51:59 · 136 阅读 · 0 评论 -
【PAT甲级 树的遍历 C++】1086 Tree Traversals Again (25 分)
第一次:测试点4错误经过一两个小时的检查发现,是输入数据的方法错了,算法完全没错:getline(cin, cmd);int x = *cmd.rbegin() - '0'; // 呵呵,sb才会这么写,哈哈哈我是sb后来想了一下,噢,原来结点数超过个位数时,上面这种输入方法就会完全错误,原来是这样啊,哈哈哈哈哈哈哈,***,wcnmlgb,我特么的这一两个小时郁闷了好久以为自己的算法有错,结果发现只是输入的错……顿时心态爆炸。下次小心吧hhhhh# include <bits/stdc原创 2021-02-20 12:42:16 · 221 阅读 · 0 评论 -
【PAT甲级 拓扑排序(假的) C++】1146 Topological Order (25 分)
不用你写拓扑排序的排序函数,只需要直到拓扑排序的一条性质:在未加入拓扑排序序列的顶点中,入度为0的顶点全都属于拓扑排序序列。然而因为可能会有多个入度为零的顶点,然后在删边的时候仍会有入度为0的顶点,所以会产生多个拓扑排序序列# include <iostream># include <vector># include <queue># include <algorithm>using namespace std;const int INF原创 2021-02-19 17:34:44 · 217 阅读 · 1 评论 -
PAT甲级个人题解表
根据《算法笔记》的章节顺序第3章 入门模拟简单模拟1002 A+B for Polynomials (25分)1009 Product of Polynomials (25分)乙级 1008 数组元素循环右移问题 (20分)查找元素1006 Sign In and Sign Out (25分)1011 World Cup Betting (20分)进制转换乙级1022 D进制的A+B (20分)1015 Reversible Primes (20分)字符串处理原创 2021-02-17 09:30:24 · 207 阅读 · 0 评论 -
【PAT甲级 单源最短路径】1087 All Roads Lead to Rome (30 分)
字符串索引图,使用了map映射,用了很多知识,能这么写出来很开心这么写可以使得map的默认值INF而不是值初始化的0struct defaultCost{ int cost; defaultCost():cost(INF){}; // 更改不存在key的map的默认value值为INF defaultCost(int c):cost(c){}};map<string, defaultCost> cost; // 之前的dist数组,改名了当点的编号不是数原创 2021-02-15 22:32:05 · 201 阅读 · 0 评论 -
【PAT甲级 最短路径 C++】1072 Gas Station (30 分)
Dijkstra# include <iostream># include <algorithm># include <numeric># include <vector># include <set>using namespace std;/* 大约就是加油站要覆盖所有的居民区,然后挑选出距离加油站最近的居民楼的距离最大的加油站(大约这样污染较少) 如果这个最大最近距离相等的话,就选择加油站到所有居民楼的平均距离更小的那个加油原创 2021-02-15 20:34:22 · 170 阅读 · 0 评论