![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
PAT (Advanced Level) Practice
题解自用
hqy_240603
这个作者很懒,什么都没留下…
展开
-
PAT甲级_1085 Perfect Sequence (25 分)
题目描述思路:就是前后下标双重循环代码:#include <bits/stdc++.h>using namespace std; int main(){ int n, p, a[100010]; cin>>n>>p; for(int i=0;i<n;i++) cin>>a[i]; sort(a, a+n); int i=0, j=0, cnt=0;//初值全赋为0 while(i<n){ while(j<n&a原创 2021-07-31 23:49:31 · 137 阅读 · 4 评论 -
PAT甲级_1083 List Grades (25 分)
题目描述思路:就是结构体排序代码:#include <bits/stdc++.h>using namespace std; struct Stu{ string name, id; int grade;};Stu stu[500];int cmp(Stu a, Stu b){ return a.grade>b.grade;}int main(){ int n, lo, hi; cin>>n; for(int i=0;i<n;i++)原创 2021-07-23 16:55:21 · 102 阅读 · 0 评论 -
PAT甲级_1082 Read Number in Chinese (25 分)
题目描述思路:按节处理,4位一节,每节末尾是万或亿,中间是十百千代码:#include <bits/stdc++.h>using namespace std; char num[10][5] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba","jiu"};char wei[5][5] = {"Shi", "Bai", "Qian", "Wan", "Yi"};int main(){ string s;原创 2021-07-23 16:01:40 · 93 阅读 · 0 评论 -
PAT甲级_1065 A+B and C (64bit) (20 分)
题目描述思路:double有效位只有15位,用long double可保证精确度代码:#include <bits/stdc++.h>using namespace std; int main(){ int T; cin>>T; long double A, B, C; for(int i=1;i<=T;i++){ cin>>A>>B>>C; cout<<"Case #"<<i<原创 2021-07-23 13:29:50 · 83 阅读 · 0 评论 -
PAT甲级_1030 Travel Plan (30 分)
题目描述思路:最短路径, 距离相等选花费最小,打印路径代码:#include <bits/stdc++.h>using namespace std; int n, m, st, ed, G[510][510], cost[510][510];int d[510], c[510], pre[510], vis[510];void dijkstra(int s){//找出路径 fill(d, d+510, INT_MAX); fill(c, c+510, INT_MAX);原创 2021-07-23 11:59:22 · 97 阅读 · 0 评论 -
PAT甲级_1026 Table Tennis (30 分)
题目描述思路:好麻烦的模拟。。。需要注意的是如果有空闲则选择编号最小的,否则选等待时间最短的代码:#include <bits/stdc++.h>using namespace std; struct player{ int at, st, tt;//到达,开始,所需 int flg; };player now;struct table{ int endt, nump; int flg;};table tb[111];vector<player>原创 2021-07-13 01:55:19 · 188 阅读 · 0 评论 -
PAT甲级_1014 Waiting in Line (30 分)
题目描述思路:时间换算成分钟,结构体是窗口代码:#include <bits/stdc++.h>using namespace std; int n, m, k, que, q;int conv(int h, int m){//时间单位转换为分钟方便处理 return h*60+m;} struct win{ int endt, popt;//最后服务时间和开始结束时间 queue<int> q;}; win w[20]; int ans[1111原创 2021-07-12 23:04:38 · 200 阅读 · 0 评论 -
PAT甲级_1079 Total Sales of Supply Chain (25 分)
题目描述思路:就是数的遍历代码:#include <bits/stdc++.h>using namespace std; struct node{ double data; vector<int> ch;};node tr[100010];int n;double p, r, ans=0;void dfs(int idx, int dep){ if(!tr[idx].ch.size()){ ans += tr[idx].data*pow(1+r, d原创 2021-06-30 11:24:17 · 96 阅读 · 0 评论 -
PAT甲级_1078 Hashing (25 分)
题目描述思路:模拟平方探测代码:#include <bits/stdc++.h>using namespace std; int isPrime(int n){ if(n<=1) return 0; for(int i=2;i<=(int)sqrt(1.0*n);i++) if(n%i==0) return 0; return 1;}int Hash[11111] = {0};int main(){ int n, tsize; cin>>原创 2021-06-29 17:59:54 · 104 阅读 · 0 评论 -
PAT甲级_1075 PAT Judge (25 分)
题目描述思路:规则有点麻烦的结构体排序代码:#include <bits/stdc++.h>using namespace std; struct Stu{ int id, sc[6], tot, cnt, flg;//id,各题分,总分,全对数,是否有效 };Stu stu[10010];int n, k, m;int full[6];int cmp(Stu a, Stu b){ if(a.tot!=b.tot) return a.tot>b.tot;//按总原创 2021-06-29 16:27:11 · 112 阅读 · 0 评论 -
PAT甲级_1074 Reversing Linked List (25 分)
题目描述思路:链表处理代码:#include <bits/stdc++.h>using namespace std; #define maxn 100010 struct node{ int add, data, nxt; int idx;//节点序号,无效为maxn//便于排序 };node l[100010];int cmp(node a, node b){ return a.idx<b.idx;}int main(){ for(int i=0;i原创 2021-06-25 11:03:31 · 85 阅读 · 0 评论 -
PAT甲级_1071 Speech Patterns (25 分)
题目描述思路:string操作,逐一检查当前字符,合法即加入当前单词,遇到非法字符则当前单词结束,跳过连续的非法字符代码:#include <bits/stdc++.h>using namespace std; int check(char c){ if((c>='0'&&c<='9')||(c>='A'&&c<='Z')||(c>='a'&&c<='z')) return 1; return原创 2021-06-21 18:45:07 · 71 阅读 · 0 评论 -
PAT甲级_1070 Mooncake (25 分)
题目描述思路:注意给出的是总价,算出单价后先卖单价最高的即可,另外注意都要用double代码:#include <bits/stdc++.h>using namespace std; struct Mcake{ double inv, pri, pp;//库存,总价,单价 };Mcake mc[1010]; int cmp(Mcake a, Mcake b){ return a.pp>b.pp;}int main(){ int n; double原创 2021-06-21 17:20:23 · 146 阅读 · 0 评论 -
PAT甲级_1067 Sort with Swap(0, i) (25 分)
题目描述思路:贪心,非0数字回到本位后不再移动,0若在i号位则与i交换,否则和不在本位的最小的数交换,把0换离本位,直至所有数都归位代码:#include <bits/stdc++.h>using namespace std; int pos[100010];//各数字当前所处位置编号 int main(){ int n, cnt=0;//交换次数 cin>>n; int all=n-1, tmp;//all为除0外不在本位的数的个数 for(int i原创 2021-06-21 16:13:06 · 72 阅读 · 0 评论 -
PAT甲级_1063 Set Similarity (25 分)
题目描述思路:就是set操作代码:#include <bits/stdc++.h>using namespace std; vector<set<int> > v;int main(){ int n; int m, tmp; ios::sync_with_stdio(false); cin>>n; for(int i=0;i<n;i++){ set<int> s; cin>>m; f原创 2021-06-17 10:12:08 · 236 阅读 · 0 评论 -
PAT甲级_1062 Talent and Virtue (25 分)
题目描述思路:规则乱七八糟的结构体排序,排序规则都写在cmp里就完事了代码:#include <bits/stdc++.h>using namespace std; struct Stu{ string id; int vir, tal, sum; int flg;//类别 };Stu stu[100010];bool cmp(Stu a, Stu b){ if(a.flg!=b.flg) return a.flg<b.flg;//类别小的在前 if(a.su原创 2021-06-15 19:36:41 · 96 阅读 · 0 评论 -
PAT甲级_1060 Are They Equal (25 分)
这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入欢迎使用Markdown编辑器你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Mar原创 2021-06-11 20:46:38 · 92 阅读 · 1 评论 -
PAT甲级_1059 Prime Factors (25 分)
题目描述思路:质因数分解,注意特判情况代码:#include <bits/stdc++.h>using namespace std;#define ll long longint isprime(ll x){ if(x==2) return 1; for(ll i=2;i<=(ll)sqrt(1.0*x);i++) if(x%i==0) return 0;//注意sqrt写法 return 1;}vector<ll> p;//n所有的质数因子 voi原创 2021-06-09 19:09:23 · 62 阅读 · 0 评论 -
PAT甲级_1056 Mice and Rice (25 分)
题目描述思路:结构体数组存老鼠信息,把其编号放入队列,方便每轮增删代码:#include <bits/stdc++.h>using namespace std;struct mice{ int w, idx, rank;};mice m[1010];queue<int> q;int main(){ int np, ng; cin>>np>>ng; for(int i=0;i<np;i++) cin>>m[i]原创 2021-06-08 22:02:01 · 69 阅读 · 0 评论 -
PAT甲级_1055 The World‘s Richest (25 分)
题目描述思路:结构体排序代码:#include <bits/stdc++.h>using namespace std;struct Rich{ string name; int age, net;}; Rich r[100010];int cmp(Rich a, Rich b){ if(a.net!=b.net) return a.net>b.net; else if(a.age!=b.age) return a.age<b.age; else retu原创 2021-06-08 18:25:26 · 84 阅读 · 0 评论 -
PAT甲级_1052 Linked List Sorting (25 分)
题目描述思路:谁能想到竟会有无效节点呢 ( ╯-_-)╯┴—┴╮(๑•́ ₃•̀๑)╭代码:#include <bits/stdc++.h>using namespace std;struct Node{ int add, key, nxt, flg;}; Node l[100010];map<int,int> mp;int cmp(Node a, Node b){ return a.key<b.key;}int main(){ int n原创 2021-06-08 17:45:39 · 81 阅读 · 0 评论 -
PAT甲级_1051 Pop Sequence (25 分)
题目描述思路:直接用栈模拟,栈顶与当前元素相同则pop,否则push,若不符也不能push则说明该序列不可能代码:#include <bits/stdc++.h>using namespace std;int main(){ int m, n, k; cin>>m>>n>>k; int cnt, flg, tmp; for(int i=0;i<k;i++){ cnt = 0; stack<int> st;原创 2021-06-08 15:58:10 · 95 阅读 · 0 评论 -
PAT甲级_1048 Find Coins (25 分)
题目描述思路:排序,用lower_bound代码:#include <bits/stdc++.h>using namespace std;int a[100010];int main(){ int n, m; cin>>n>>m; for(int i=0;i<n;i++) cin>>a[i]; sort(a, a+n); int v1=0, v2=0; for(int i=0;i<n;i++){ int j =原创 2021-06-08 15:09:53 · 66 阅读 · 0 评论 -
PAT甲级_1047 Student List for Course (25 分)
题目描述思路:最后一个测试点真心没意思,用set会超时,用cincout也超,必须加上.c_str(),并且之前要resize。。倒也不必这么恶心人,写代码十分钟改超时一个半小时代码:#include<bits/stdc++.h>using namespace std;map<int,vector<string>> mp;int main() { int n,k; cin>>n>>k; string name;原创 2021-06-08 14:21:37 · 107 阅读 · 0 评论 -
PAT甲级_1044 Shopping in Mars (25 分)
题目描述思路:值非负,故从1到i的累加数组递增,左端点枚举右端点二分,整体复杂度O(nlogn)代码:#include <bits/stdc++.h>using namespace std;int sum[100010];//a[i]非负故sum[i]递增 int main(){ int n, m; cin>>n>>m; for(int i=1;i<=n;i++){//为便于输出使i从1起 cin>>sum[i];原创 2021-06-08 11:39:50 · 55 阅读 · 0 评论 -
PAT甲级_1040 Longest Symmetric String (25 分)
题目描述思路:边界是中间一个或两个为回文串,然后长度从3起,若中间是两端也是则长度更新代码:#include <bits/stdc++.h>using namespace std;int dp[1010][1010];//d[i][j]==1表示[i,j]为回文子串 int main(){ string s; getline(cin, s); int len=s.length(), ans=1; for(int i=0;i<len;i++) dp[i][i]=1原创 2021-06-07 15:04:56 · 73 阅读 · 0 评论 -
PAT甲级_1039 Course List for Student (25 分)
题目描述思路:用map和set存,很便捷代码:#include <bits/stdc++.h>using namespace std;map<string, set<int> > mp;int main(){ int n, k; cin>>n>>k; int idx, num; string name; for(int i=0;i<k;i++){ cin>>idx>>num; fo原创 2021-06-07 10:34:42 · 85 阅读 · 0 评论 -
PAT甲级_1037 Magic Coupon (25 分)
题目描述思路:排序,从两侧绝对值最大的开始乘,只选积为正数的组合代码:#include <bits/stdc++.h>using namespace std;int cou[100010], pro[100010];int main(){ int nc, np; cin>>nc; for(int i=0;i<nc;i++) cin>>cou[i]; cin>>np; for(int i=0;i<nc;i++) cin&原创 2021-06-06 19:09:58 · 77 阅读 · 0 评论 -
PAT甲级_1036 Boys vs Girls (25 分)
题目描述思路:输出女生最高分-男生最低分的值,分别排序即可代码:#include <bits/stdc++.h>using namespace std;struct Stu{ string name; string id; int score;}; Stu f[1000], m[1000]; int cmp(Stu a, Stu b){ return a.score < b.score;}int main(){ int n; cin>>n原创 2021-06-06 14:28:33 · 82 阅读 · 2 评论 -
PAT甲级_1033 To Fill or Not to Fill (25 分)
题目描述思路:加油站排在一条直线上,若当前无可达则结束,若可达范围内有比当前更低价站则当前油只要够到该站即可,否则在当前站加满,下一站去可达范围内价格最低站代码:#include <bits/stdc++.h>using namespace std;struct Sta{//注意都是double double pri; double dis;}; Sta sta[505]; int cmp(Sta a, Sta b){ return a.dis<b.dis;}原创 2021-06-05 20:16:29 · 81 阅读 · 0 评论 -
PAT甲级_1032 Sharing (25 分)
题目描述思路:结构体存next和flag,第一个链表中flag置1,然后遍历第二个链表,遇到flag为1的点则停止代码:#include <bits/stdc++.h>using namespace std;struct Node{//数据域其实没用 int nxt; int flg;//在第一个链表中出现则为1 }; Node node[100010]; int main(){ int bg1, bg2, n; for(int i=0;i<10010;i原创 2021-06-05 17:30:20 · 64 阅读 · 0 评论 -
PAT甲级_1009 Product of Polynomials (25 分)
题目描述思路:存储第一个多项式,第二个多项式读入时直接与第一个逐项相乘,系数存入结果数组,即两个指数之和的位置加上两个系数之积代码:#include <bits/stdc++.h>using namespace std;struct poly{ int exp; double coe;}; poly a[1001];double ans[2002];//结果多项式从高到低的系数 int main(){ int k1, k2; cin>>k1; fo原创 2021-06-05 10:13:39 · 88 阅读 · 0 评论 -
PAT甲级_1025 PAT Ranking (25 分)
题目描述思路:结构体存储考生,各组在读入时排序,总排序在输出时得到代码:#include <bits/stdc++.h>using namespace std;struct Stu{ char id[15]; int score; int lonum; int lorank;}; Stu stu[30010]; int cmp(Stu a, Stu b){//分数由高到低,准考证从小到大 if(a.score!=b.score) return a.score>原创 2021-06-05 11:46:25 · 55 阅读 · 0 评论 -
PTA_PAT甲级_1091 Acute Stroke (30分)
One important factor to identify acute stroke (急性脑卒中) is the volume of the stroke core. Given the results of image analysis in which the core regions are identified in each MRI slice, your job is to calculate the volume of the stroke core.Input Specificat原创 2020-06-05 17:11:26 · 139 阅读 · 0 评论 -
PTA_PAT甲级_1087 All Roads Lead to Rome (30分)
Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.Input Specification:Each input file contains one test case. For each case, the firs原创 2020-06-04 13:20:04 · 180 阅读 · 0 评论 -
PTA_PAT甲级_1080 Graduate Admission (30分)
It is said that in 2011, there are about 100 graduate schools ready to proceed over 40,000 applications in Zhejiang Province. It would help a lot if you could write a program to automate the admission procedure.Each applicant will have to provide two grad原创 2020-06-02 21:43:54 · 136 阅读 · 0 评论 -
PTA_PAT甲级_1076 Forwards on Weibo (30分)
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all his/her followers can vi原创 2020-06-02 21:29:48 · 138 阅读 · 0 评论 -
PTA_PAT甲级_1072 Gas Station (30分)
A gas station has to be built at such a location that the minimum distance between the station and any of the residential housing is as far away as possible. However it must guarantee that all the houses are in its service range.Now given the map of the c原创 2020-06-02 16:09:08 · 177 阅读 · 0 评论 -
PTA_PAT甲级_1068 Find More Coins (30分)
Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for eac原创 2020-06-01 17:48:57 · 204 阅读 · 0 评论 -
PTA_PAT甲级_1057 Stack (30分)
Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to im原创 2020-06-01 10:42:56 · 159 阅读 · 0 评论