前言:距离四级考试剩23天,PAT甲级考试剩24天
对PAT甲级练习题做总结

1042 Shuffling Machine (20 分)

  • 题目大意:
    重复给出排列方式,把放在下标 i i i的牌调换到下标 j j j
  • 题目解析:
    模拟题

英语单词:

shuffle 洗牌,侧滑
machine 机器,机械
procedure 程序,手续,过程,步骤
deck 平台,甲板
be seen as 被认为是,被看作
gamblers 赌徒
collaborate 合作,协作
inadequate 不充分的,不适当的
casino 赌场
automatic 自动的
simulate 模拟,假装
initial 初始的,最初的
in the following 在下文中,在下面
spade 铁锹,铲子
heart 心
club 俱乐部
diamond 钻石,方块
joker 小丑,大小王
stand for 代表

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 1e3+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/int n,k;
int pos[N];
string alp = "SHCD";
vector<string> t1,t2;void init(){int cnt = 0;for(int j = 0; j < 4; j ++ ){for(int i = 1; i <= 13; i ++ ){string t = alp[j] + to_string(i);t1.push_back(t);}}for(int i = 1; i <= 2; i ++ ) t1.push_back("J" + to_string(i));
}int main(){cin >> k;for(int i = 0; i < 54; i ++ ) cin >> pos[i];init();while(k --){t2 = t1;for(int i = 0; i < 54; i ++ ) t2[pos[i] - 1] = t1[i];t1 = t2;}for(int i = 0; i < 54; i ++ ){if(i) cout<<" ";cout<<t1[i];}return 0;
}

1043 Is It a Binary Search Tree (25 分)

  • 题目大意:
    给出前序,本身或者镜像是BST就OJK,并输出后序
  • 题目解析:
    二叉树问题

英语单词:

Binary Search Tree 二叉搜索树
recursively 递归地
mirror 镜子,镜面
preorder 前序
inorder 中序
postorder 后序

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 1e3+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/int n;
int a[N];
vector<int> post;
bool isok;void dfs(int l,int r){if(l > r) return ;int x = a[l];int i = l + 1,j = r;if(isok){while(i <= r && a[i] < x) i ++;while(j > l && a[j] >= x) j --;}else{while(i <= r && a[i] >= x) i ++;while(j > l && a[j] < x) j --;}if(i - j != 1) return ;dfs(l+1,j);dfs(i,r);post.push_back(x);
}void print(){bool ok = false;for(auto x : post) {if(ok) cout<<" ";ok=true;cout<<x;}
}
int main(){cin >> n;for(int i = 0; i < n; i ++ ) cin >> a[i];isok = true;dfs(0,n - 1);if(post.size() != n){isok = false;post.clear();dfs(0,n - 1);}if(post.size() == n) isok = true;if(isok) {puts("YES");print();}else puts("NO");return 0;
}

1044 Shopping in Mars (25 分)

  • 题目大意:
    找到连续子数组之和>=给定值,取最小的值
  • 题目解析:
    求前缀和,因为前缀和数组是非递减的。因此,可以用二分找到>=给定值的最小值。

英语单词:

diamond 钻石
take off 起飞,脱下,离开
exact 精确的,准确的
This is sufficient to 能够这样就足够了
sufficient 足够的,充分的,充足的

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 1e5+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/int n,m;
int a[N],s[N];int main(){ios::sync_with_stdio(false);cin.tie(0);cin>>n>>m;for(int i = 1; i <= n; i ++ ) cin >> a[i];for(int i = 1; i <= n; i ++ ) s[i] = s[i - 1] + a[i];map<int,vector<pii>> v;for(int l = 1; l <= n; l ++ ){int r = lower_bound(s+1,s+n+1,m+s[l-1]) - s;if(r > n) continue;int sum = s[r]-s[l-1];v[sum].push_back({l,r});}for(auto t : v){vector<pii> ans = t.y;for(auto [l,r] : ans) cout<<l<<"-"<<r<<endl;break;}return 0;
}

1045 Favorite Color Stripe (30 分)

  • 题目大意:
    很有意思的一道题,普通人的眼睛可以辨别的颜色不超过200种,我只想挑选我喜欢的颜色,其他的不管。喜欢的颜色按照喜欢的顺序排放。找出满足这些条件的最长子序列。
  • 题目解析:
    最长上升子序列问题。因为按照指定的顺序排列,因此,我们这里需要存储下标来计算。

英语单词:

stripe 线条,条纹
the remaining parts 剩余的部分

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 1e5+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/int n,m;
int c[N],a[N],f[N];
int cnt;int main(){ios::sync_with_stdio(false);cin.tie(0);cin >> m>> n;for(int i = 1; i <= n; i ++ ){int x; cin >> x;c[x] = i;}cin >> m;for(int i = 1; i <= m; i ++ ){int x; cin >> x;if(c[x] >= 1) a[++cnt] = c[x];}int mx = -1;for(int i = 1; i <= cnt; i ++ ){f[i] = 1;for(int j = 1; j < i; j ++ ){if(a[i] >= a[j]) f[i] = max(f[i],f[j] + 1);}mx = max(mx,f[i]);}cout<<mx;return 0;
}

1046 Shortest Distance (20 分)

  • 题目大意:
    找出两个点的最短距离
  • 题目解析:
    可以把这些点看作一维平面上相邻的点,用2倍N来模拟循环。查询的是点,存的是边距离,找出相应的下标。

英语单词:

the total round trip distance 全部往返总距离

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 2e5+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/int n,m;
int a[N],s[N];int main(){ios::sync_with_stdio(false);cin.tie(0);cin >> n;for(int i = 1; i <= n; i ++ ) cin >> a[i],a[i+n] = a[i];for(int i = 1; i <= 2*n; i ++ ) s[i] = s[i - 1] + a[i];cin >> m;while(m --){int a,b;cin>>a>>b;if(a > b) swap(a,b);cout<<min(s[b-1]-s[a-1],s[a+n-1]-s[b-1])<<endl;}return 0;
}

1047 Student List for Course (25 分)

  • 题目大意:
    话不多说看代码。

  • 题目解析:
    模拟题。

英语单词:

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 2e5+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/int n,m;int main(){cin >> n >> m;map<int,vector<string>> info;for(int i = 0; i < n; i ++ ){string s;int k;cin>>s>>k;for(int j = 0; j < k; j ++ ){int x; cin >> x;info[x].push_back(s);}}for(int i = 1; i <= m; i ++ ){int id = i;vector<string> v = info[id];sort(v.begin(),v.end());cout<<id<<" "<<v.size()<<endl;for(auto s : v) printf("%s\n",s.c_str());}return 0;
}

1048 Find Coins (25 分)

  • 题目大意:
    找出一对硬币满足给定值,如果找不到,输出No Solution.

  • 题目解析:
    当前值是a,和是sum,得出另一个值是sum-a。判断是否存在并且下标不一样就可。

英语单词:

definitely 当然地,肯定的,确实
whether or not 无论,是否
no solution 无解
multiple solutions 多解

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 2e5+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/int n,m;
int a[N];
map<int,int> hs;int main(){cin >> n >> m;for(int i = 0; i < n; i ++ ) cin >> a[i];sort(a,a+n);for(int i = 0; i < n; i ++ ) hs[a[i]] = i;bool ok = false;for(int i = 0; i < n; i ++ ){if(hs[m - a[i]] && i != hs[m-a[i]]) {ok=true;cout<<a[i]<<" "<<m-a[i]<<endl;break;}}if(!ok) puts("No Solution");return 0;
}

1049 Counting Ones (30 分)

  • 题目大意:
    找出1到N中,所有数字位上的1
  • 题目解析:
    先暴力混分吧,回头再看看

英语单词:

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 2e5+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/
int n;int cal(int n){int cnt = 0;while(n){if(n%10==1) cnt++;n/=10;}return cnt;
}int main(){cin>>n;int cnt = 0;for(int i = 1; i <= n; i ++ ){cnt += cal(i);}cout<<cnt;return 0;
}

1050 String Subtraction (20 分)

  • 题目大意:
    在S1中,输出字符串S2没有出现过的字母
  • 题目解析:
    标记辣

英语单词:

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 2e5+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/int main(){string a,b;getline(cin,a);getline(cin,b);map<char,bool> hs;for(auto c : b) hs[c] = true;for(auto c : a) if(!hs[c]) cout<<c;return 0;
}

1051 Pop Sequence (25 分)

  • 题目大意:

  • 题目解析:
    合法输出出栈序列问题。
    栈 - 关于出栈序列,判断合法的出栈序列

英语单词:

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 1e5+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
*/int n,m,k;
int stk[N],a[N];
int tot;int main(){cin >> n >> m >> k;while(k --){tot = 0;for(int i = 1; i <= m; i ++ ) cin >> a[i];bool ok = true;int cur = 1;for(int i = 1; i <= m; i ++ ){stk[++tot] = i;if(tot > n) ok = false;while(tot && stk[tot] == a[cur]) tot--,cur ++;}if(ok && !tot) puts("YES");else puts("NO");}return 0;
}

1052 Linked List Sorting (25 分)

  • 题目大意:
    原链表结点的值按照递增排序调整。
  • 题目解析:
    根据给出的头节点,找出这个链表,建立一个新链表输出。
  • 注意:
    给出的结点,不一定在头节点的链表上。读题读不出来还有这样的输入,但是又合情合理,他又不说。如果不这样,我直接排个序输出不就OJK了?

英语单词:

necessarily 必要的
adjacent 相邻的

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 1e6+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/*
0      1000  -1   10000  100
00001-22222-12345-33333-11111-1     0      100   1000 10000
12345 00001 11111 22222  33333
*/int n,m;
int h1,ne[N];
map<int,int> we,ew,hs;int main(){cin >> n >> h1;for(int i = 1; i <= n; i ++ ){int a,c,b;cin>>a>>b>>c;ne[a] = c;we[b] = a;ew[a] = b;}if(h1 == -1) {puts("0 -1");return 0;}while(h1 != -1){hs[h1] = 1,h1 = ne[h1];}vector<int> v;for(auto t : we){if(hs[t.y]) v.push_back(t.y);}int nn = v.size();printf("%d %05d\n",nn,v[0]);for(int i = 0; i < nn; i ++ ){if(i != nn-1) printf("%05d %d %05d\n",v[i],ew[v[i]],v[i+1]);else printf("%05d %d %d\n",v[i],ew[v[i]],-1);}return 0;
}

1053 Path of Equal Weight (30 分)

  • 题目大意:
    找出从根节点到叶子结点权值和是给定值的所有路径
  • 题目解析:
    dfs辣
  • 注意:所有儿子结点按照非递减的顺序排放,这样能保证跟输出格式一致

英语单词:

leaf node 叶子结点
sake 利益,好处;目的;为了便于讨论
For the sake of simplicity 为了简单起见
extra 额外的

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 1e3+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/**/
int n,k,m;
int w[N];
vector<int > v[N];
bool vis[N];
int mx;
vector<int> tmp;
vector<vector<int>> ans;void dfs(int u,int sum){if(v[u].size() == 0){if(sum == m) ans.push_back(tmp);return ;}for(int j : v[u]){if(vis[j]) continue;vis[j] = true;tmp.push_back(w[j]);dfs(j,sum + w[j]);vis[j] = false;tmp.pop_back();}
}int main(){cin >> n >> k >> m;    for(int i = 0; i < n; i ++ ) cin >> w[i];while(k --){int id,kk; cin >> id >> kk;vector<pii> t;while(kk --){int x;cin>>x;t.push_back({w[x],x});}sort(t.begin(),t.end(),greater<>());for(auto &[k,x] : t) v[id].push_back(x);}tmp.push_back(w[0]);dfs(0,w[0]);for(auto t : ans){bool ok = false;for(auto x : t) {if(ok) cout<<" ";cout<<x;ok=true;}puts("");}return 0;
}

1054 The Dominant Color (20 分)

  • 题目大意:
    严格主色:找出占超过一半区域的颜色
  • 题目解析:
    找最大的那个就可,因为题目保证有,也不可以是俩辣

英语单词:

dominant adj. 占支配地位的,占优势的;(基因)显性的
scene 场景,场面
a series 一系列
pixel 像素
be called 被称为
proportional 成比例的
strictly 严格的,完全的,确实的
an image of resolution 图片分辨率

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 1e3+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/**/int n,m;
int main(){map<int,int> cnts;cin >> n >> m;for(int i = 1; i <= n*m; i ++ ){int x; cin>>x;cnts[x] ++;}int mx = -1,mx_id = -1;for(auto &[k,v] : cnts){if(v > mx) mx = v,mx_id = k;}cout<<mx_id;return 0;
}

1055 The World’s Richest (25 分)

  • 题目大意:

  • 题目解析:

英语单词:

代码如下:

#include<bits/stdc++.h>using namespace std;const int N = 1e3+10;
const int inf = 0x3f3f3f3f;#define x first
#define y secondtypedef pair<int,int> pii;
typedef double db;/**/
int n,k;
map<string,vector<pii>> info;
struct Node{string name;int age,w;
};bool cmp1(Node a,Node b){if(a.w!=b.w) return a.w>b.w;if(a.age!=b.age) return a.age<b.age;return a.name<b.name;
}bool cmp2(Node a,Node b){if(a.age!=b.age) return a.age<b.age;
}int main(){cin >> n >> k;vector<Node> v(n);for(int i = 0; i < n; i ++ ){string id;int a,b;cin>>id>>a>>b;v[i] = {id,a,b};}sort(v.begin(),v.end(),cmp2);vector<int> val;for(auto t : v) val.push_back(t.age);sort(val.begin(),val.end());int c = 1;while(k --){int m,l,r;cin>>m>>l>>r;int L = lower_bound(val.begin(),val.end(),l)-val.begin();int R = upper_bound(val.begin(),val.end(),r)-val.begin();R--;vector<Node> ans;for(int i = L; i <= min(L+m,R); i ++ ) ans.push_back(v[i]);printf("Case #%d:\n",c++);// cout<<L<<" "<<R<<endl;// for(int i = L; i <= R; i ++ ) cout<<val[i]<<" ";// puts("");sort(ans.begin(),ans.end(),cmp1);bool ok = true;int nn = ans.size();for(int i = 0; i < min(m,nn); i ++ ){ok = false;Node t = ans[i];printf("%s %d %d\n",t.name.c_str(),t.age,t.w);}if(ok) puts("None");}return 0;
}

PAT甲级1042~1055相关推荐

  1. PAT甲级1042 Shuffling Machine:[C++题解]模拟、哈希表、洗牌机

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:序列置换. 这里用到函数memcpy()用于数组复制,用法memcpy(dest, src, sizeof dest); 把src数组 ...

  2. PAT甲级 1042 Shuffling Machine 模拟洗牌 map的使用

    Solution: 题目要求:要去实现一个模拟洗牌的程序.具体要求如下: (1)初始的排序已经给出. (2)要求洗k次牌,并输出最终排序. (3)给出54个数字,如果第i个位置上的数字为j,就要把第i ...

  3. PAT甲级1055 The World‘s Richest:[C++题解]k路归并

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 采用二维数组vector[N]来存每个年龄的人(结构体),然后分别从大到小排序.剩下的任务就是从给定的年龄[a ,b]中,k路归并最 ...

  4. PAT甲级题目对应知识点分类梳理

    PAT甲级的106道题的知识点与对应的题号整理如下,便于做专项练习和巩固! 1.数据结构 可以用STL系列 栈:1051 堆:1098 队列:1014.1056 链表:1032.1052.1074.1 ...

  5. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

  6. PAT甲级训练合集(1-70)

    本章题解跳转 考点 P1001 数字的数组表示和处理 P1002 多项式的数组表示和处理 P1003 深度优先搜素 P1004 深度优先搜素 P1005 哈希表 P1006 P1007 数组子区间求和 ...

  7. PAT甲级题目解析和知识点分类整理

    转载请注明出处 个人博客:https://maxusun.github.io/ 今天整理电脑,发现了去年为了考研浙大计算机整理的PAT资料.现在考研已经尘埃落定.想到当时盲目刷题浪费了好多时间,在这里 ...

  8. PAT甲级 A1042

    PAT甲级 A1042 题目详情 1042 Shuffling Machine (20分) Shuffling is a procedure used to randomize a deck of p ...

  9. PAT甲级考试题库题目分类

    PAT甲级题目分类 水题 1136.1139.1143.1148 字符串处理 1001.1005.1035.1061.1073.1077.1082. 1108.1140.1152 模拟 1002.10 ...

最新文章

  1. JavaScript中十种一步拷贝数组的方法
  2. 改权限 chown改用户归属_域权限维持 | 改密码我也能获取你的密码 | Hook PasswordChangeNotify 攻击...
  3. Spring 从零開始-05
  4. java jstack 死锁_利用jstack检测死锁DeadLock
  5. CCNA training notes
  6. python 多进程 内存增长_python 多进程 内存 copy-on-write
  7. 利用javascript动态创建表格
  8. solr 5.0.0 bin/start脚本详细解析
  9. Miccai2019 oral简单总结
  10. 顶点计划 抄作业问题讨论
  11. C++ Primer Plus 6 第一章
  12. intel fortran免费版安装
  13. cad脚本合适_CAD脚本学习
  14. 利用rpm命令安装软件
  15. logisitic 回归 +极大似然法 + 梯度下降法 (迭代优化)
  16. 004 ZeroMQ PUB XSUB-XPUB SUB模式
  17. 【数据可视化应用】实现空间栅格(附R语言代码)
  18. 统计3个班成绩情况,每个班有5名同学 求出各个班的平均分和所有班级的平均分(学生成绩键盘输入) 同级生哪个班及格人数,每个班5名同学
  19. 浏览器中调试web你知道吗?
  20. 联想小新锐7000安装win10和ubuntu18.04双系统踩坑

热门文章

  1. springmvc的RequestPartMethodArgumentResolver解析_晏无心_新浪博客
  2. html获得文本框的值,jQuery中怎么获取文本框的值?
  3. 一辈子坚持只做一件事
  4. 如何使用Kali破解WIFI密码蹭网?
  5. Windows11通过wsl2安装linux图形界面
  6. 8086CPU与80386CPU的工作原理区别
  7. 每日学术速递2.17
  8. 自学python的日记分享
  9. Linux系统安装IDEA保姆级教程
  10. 科创板开市两周年 | 2021最具创新力科创板上市公司、最具价值科创板上市公司等榜单揭晓...