PAT1138 Postorder Traversal

  根据前序和中序遍历确定二叉树,模板题,要求输出第一个后序遍历的节点。TIPS:利用map来映射后序遍历在中序遍历中的位置,否则复杂度过高导致TLE。

#include<iostream>
#include<unordered_map>#define ac cin.tie(0);cin.sync_with_stdio(0);
using namespace std;
const int MAXN = 50010;
int in[MAXN], pre[MAXN];
int N;
unordered_map<int, int> M;int main() {accin >> N;for (int i = 0; i < N; i++)cin >> pre[i];for (int i = 0; i < N; i++) {cin >> in[i];//提高效率 否则TLEM[in[i]] = i;}int le = 0, ri = N - 1, k = 0;while (le < ri) {int pos = M[pre[k]];k++;//有左孩子if (pos > le)ri = pos - 1;//否则找右孩子elsele = pos + 1;}cout << in[le];return 0;
}

PAT 1053 Path of Equal Weight

  DFS回溯模板题,注意Pat的数据较弱,如果样例很大,显然这个做法会TLE。题目要求权值从大到小,所以在每个节点存储的临界点中,按照权值从大到小dfs即可。

#include<iostream>
#include<vector>
#include<algorithm>using namespace std;
const int MAXN = 110;
//邻接表存边
vector<int> G[MAXN], v;
vector<vector<int>> ans;
int we[MAXN];
int N, M, K;bool cmp(int a, int b) {return we[a] > we[b];
}void dfs(int root, int &left) {v.push_back(we[root]);if (G[root].empty()) {if (left == we[root]) {ans.push_back(v);}} else {left -= we[root];for (int i:G[root])dfs(i, left);left += we[root];}v.pop_back();
}int main() {int a, len, tmp;cin >> N >> M >> K;for (int i = 0; i < N; i++)cin >> we[i];while (M--) {cin >> a >> len;for (int i = 0; i < len; i++) {cin >> tmp;G[a].push_back(tmp);}sort(G[a].begin(), G[a].end(), cmp);}dfs(0, K);for (int i = 0; i < ans.size(); i++) {if (i)cout << endl;for (int j = 0; j < ans[i].size(); j++) {if (j)cout << " ";cout << ans[i][j];}}return 0;
}

PAT 1094 The Largest Generation

  树的层序遍历,模板题,注意不使用Queue来进行Level遍历的写法:

#include<iostream>#define ac cin.tie(0);cin.sync_with_stdio(0);
using namespace std;
const int MAXN = 110;
bool G[MAXN][MAXN];
int arr[MAXN];
int N, M;int main() {int root, len, cl;cin >> N >> M;while (M--) {cin >> root >> len;while (len--) {cin >> cl;G[root][cl] = true;}}int ans1 = 0, ans2 = -1;int tt = 1, hh = 1, depth = 1;arr[1] = 1;while (tt <= hh) {//size+1为当前层的个数,省去了使用Queueint size = hh - tt + 1;if (size > ans1) {ans1 = size;ans2 = depth;}for (int j = 0; j < size; j++) {root = arr[tt++];for (int i = 1; i <= N; i++)if (G[root][i])arr[++hh] = i;}++depth;}cout << ans1 << " " << ans2;return 0;
}

PAT1024 Palindromic Number

  模拟高精度,模板题。

#include<iostream>
#include<string>using namespace std;bool judge(const string &s) {for (int i = 0; i < s.length() / 2; i++)if (s[i] != s[s.length() - 1 - i])return false;return true;
}void add(string &s) {string tmp(s);int carry = 0;for (int i = s.length() - 1; i >= 0; i--) {s[i] += tmp[s.length() - 1 - i] - '0' + carry;carry = 0;if (s[i] > '9') {carry = 1;s[i] -= 10;}}//溢出的进位if (carry)s = "1" + s;
}int main() {string s;int n, i = 0;cin >> s >> n;for (; i < n; i++) {//判断是否为回文串if (judge(s))break;//相加add(s);}cout << s << endl << i;return 0;
}

PAT1075 PAT Judge

  一道很PAT的PAT题,很繁琐,要细心,通过全部测试点很不容易。

//PAT题目太烦了
#include<cstdio>
#include<algorithm>using namespace std;
int fenshu[6];struct node {int score[6], cnt, total, id;bool shown;node() {//编号id = -1;//完美样例个数cnt = 0;//总分total = 0;//该用户是否可以显示出来shown = false;for (int i = 0; i < 6; i++)//-1代表没有提交过,0代表提交了但是编译出错为0分score[i] = -1;}
} arr[10010];int N, K, M;//按照题目要求排序
bool cmp(node &a, node &b) {if (a.total != b.total)return a.total > b.total;else if (a.total == b.total && a.cnt != b.cnt)return a.cnt > b.cnt;elsereturn a.id < b.id;
}int main() {int a, b, c;scanf("%d %d %d", &N, &K, &M);for (int i = 1; i <= N; i++)arr[i].id = i;for (int i = 1; i <= K; i++)scanf("%d", &fenshu[i]);while (M--) {scanf("%d %d %d", &a, &b, &c);if (c == -1)//-1代表提交了但是为0分,与我设的-1为没提交过不符,因此在此特判下c = 0;else//只要有一个得分了,就可以显示出来arr[a].shown = true;arr[a].score[b] = max(arr[a].score[b], c);}for (int i = 1; i <= N; i++) {for (int j = 1; j <= K; j++)if (arr[i].score[j] != -1) {arr[i].total += arr[i].score[j];if (arr[i].score[j] == fenshu[j])//AC样例+1++arr[i].cnt;}}sort(arr + 1, arr + 1 + N, cmp);int pos = 1, pos2 = 0;for (int i = 1; i <= N; i++) {//跳过没有资格显示的用户if (!arr[i].shown)continue;else {++pos2;if (arr[i].total != arr[pos].total)pos = pos2;printf("%d %05d %d", pos, arr[i].id, arr[i].total);for (int j = 1; j <= K; j++)if (arr[i].score[j] != -1)printf(" %d", arr[i].score[j]);elseprintf(" -");if (i != N)printf("\n");}}return 0;
}

PAT甲级官网 刷题(1)相关推荐

  1. PAT甲级官网 刷题(3)

    PAT1130 Infix Expression   递归,但是官网有个测试点没过,不知道错在哪里,欢迎指出 #include<iostream>#define ac cin.tie(0) ...

  2. 蓝桥杯官网刷题记录python

    蓝桥杯官网刷题记录python 由于很多题都会在2020.2021.2022年省赛出现,有的在前面文章里做过的这里就不会再说了 一.空间 小蓝准备用 256MB 的内存空间开一个数组,数组的每个元素都 ...

  3. 2020年9月PAT甲级满分必备刷题技巧

    2020年7月的考试结束了,除了本次的考题更新,短期内不会更新. [7月题目的特点:首次线上考试,没出链表.树相关的模板题,第2到4题背景新颖,大大降低了抄袭历年代码的可能性,可以看作是线上考试的新趋 ...

  4. CCF官网刷题过程之中避免时间久了,网站登录失效 CCF提交代码编译出错

    每次刷题,过了一会来提交,又得重新登录,烦,就找个浏览器插件,对网页自动刷新. chrome插件 auto fresh 编译出错: 目前发现比较函数参数必须添加修饰符const,否则无法通过编译. 这 ...

  5. 【PAT甲级】A1001-A1050刷题记录

    文章目录 A1001 A+B Format (20 分) 0.25 ★(一元多项式加法) A1002 A+B for Polynomials (25 分) 0.21 (单源最短路Dijkstra+边权 ...

  6. 【PAT甲级】A1051-A1100刷题记录

    文章目录 (栈) A1051 Pop Sequence (25 分) 0.47 (静态链表) A1052 Linked List Sorting (25 分) 0.21 (静态树+先根遍历DFS) A ...

  7. 【PAT甲级】A1101-A1155刷题记录

    文章目录 (递推) A1101 Quick Sort (25 分) 0.23 (静态二叉树+遍历) A1102 Invert a Binary Tree (25 分) 0.51 (数学问题) A110 ...

  8. 牛客网 刷题前的准备工作(输入 输出 如何接收?)

    牛客网 刷题前的准备工作 牛客网 刷题前的准备工作 1. 数据读取接受问题 2.牛客刷题前的准备: 2.1. 弄清楚输入输出的行数关系 3.代码怎么写 3.1. 在牛客上测试自己的模板代码,是否能正确 ...

  9. PAT甲级(Advanced Level)真题--1046 Sharing

    PAT甲级(Advanced Level)真题–1046 Sharing 通过:648 提交:1138 通过率:56% To store English words, one method is to ...

最新文章

  1. 你还在使用 try-catch-finally 关闭资源?
  2. WebForm中关于DataGrid的20篇经典文章
  3. 阿里消息队列mq服务器,阿里的架构之路——漫谈MQ
  4. Oracle 数据库中对记录进行分页处理
  5. Mongoose之 SchemaTypes 数据类型
  6. 使用 Adobe AIR 管理 WordPress 评论
  7. 第二章--电商设计表
  8. linux 如何连接无线网卡,CentOS 7如何连接无线网络
  9. ASP.NET域集成AD身份验证
  10. linux查看cpt硬盘命令,Linux基础知识复习之命令篇
  11. NewSQL登堂入室 数据库厂商掘金行业大数据
  12. wxwindows编译
  13. 【Linux常用服务器配置——Vsftpd服务】
  14. 万能格式转换器1.2绿色免费汉化版
  15. win10时间不准_Win10实用技巧之win10系统电脑重置
  16. 2017-2018 ACM-ICPC, Asia Daejeon Regional Contest
  17. 喜讯 雨笋教育优秀学员荣获望城区网络安全攻防演练二等奖
  18. Spring之AOP 切入点表达式写法
  19. Uboot DM9621网卡移植之路
  20. dfs-全排列(UPC-方案数)

热门文章

  1. c#实现将Excel文件导出为csv和UTF8格式的txt文件
  2. Pyinstaller 4.4官方手册 3# Pyinstaller是什么?是如何工作的?
  3. [PPT] 设定PPT默认字体
  4. 【Go】超详细Go入门
  5. Go | Go和Java区别
  6. JavaScript资源大全中文版(Awesome最新版)
  7. DapperLambda发布
  8. 【财经期刊FM-Radio|2020年11月24日】
  9. Elasticsearch杂记(1)
  10. pmbok电子版_【员工亲历PMP学习分享】用技巧方法,让PMBOK知识点烂熟于心清晖安安...