PAT (Advanced Level) 1132~1135:1132 模拟 1133模拟(易超时!) 1134图 1135红黑树
1132 Cut Integer(20 分)
题意:将一个含K(K为偶数)个数字的整数Z割分为A和B两部分,若Z能被A*B整除,则输出Yes,否则输出No。
分析:当A*B为0的时候,不能被Z整除,输出No。否则会出现浮点错误。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
char s[20];
int main(){int N;scanf("%d", &N);while(N--){scanf("%s", s);int len = strlen(s);int A = 0;for(int i = 0; i < len / 2; ++i){A = A * 10 + s[i] - '0';}int B = 0;for(int i = len / 2; i < len; ++i){B = B * 10 + s[i] - '0';}int C = A * B;if(C == 0){printf("No\n");continue;}int x = atoi(s);if(x % C == 0) printf("Yes\n");else printf("No\n");}return 0;
}
1133 Splitting A Linked List(25 分)
题意:给定一个链表,将链表重新排序,在不打乱原链表相对顺序的前提下,小于0的在最前面,其次是0~K,最后是大于K的数。
分析:
1、3次遍历可实现链表重排。
2、map映射value和pre或suc的关系会超时,所以,以pre为结点定义结构体,组织链表的重排,从而进行优化。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
const int MAXN = 100000 + 10;
struct Node{int pre, value, suc;
}num[MAXN];
vector<int> old, ans;
int main(){int N, K, head, pre, value, suc;scanf("%d%d%d", &head, &N, &K);for(int i = 0; i < N; ++i){scanf("%d%d%d", &pre, &value, &suc);num[pre].pre = pre;num[pre].value = value;num[pre].suc = suc;}while(head != -1){old.push_back(head);head = num[head].suc;}int len = old.size();for(int i = 0; i < len; ++i){if(num[old[i]].value < 0) ans.push_back(old[i]);}for(int i = 0; i < len; ++i){if(num[old[i]].value >= 0 && num[old[i]].value <= K) ans.push_back(old[i]);}for(int i = 0; i < len; ++i){if(num[old[i]].value > K) ans.push_back(old[i]);}for(int i = 0; i < len - 1; ++i){printf("%05d %d %05d\n", ans[i], num[ans[i]].value, ans[i + 1]);}printf("%05d %d -1\n", ans[len - 1], num[ans[len - 1]].value);return 0;
}
1134 Vertex Cover(25 分)
题意:vertex cover是指图中一些点的集合,使得图中每一条边的两个点中都至少有一个点在该点集中。给定点的集合,判断是否为vertex cover。
分析:按题意模拟即可。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
const int MAXN = 10000 + 10;
int N, M;
bool vis[MAXN];
struct Edge{int u, v;void read(){scanf("%d%d", &u, &v);}
}num[MAXN];
int main(){scanf("%d%d", &N, &M);for(int i = 0; i < M; ++i){num[i].read();}int K;scanf("%d", &K);while(K--){int n, x;scanf("%d", &n);memset(vis, false, sizeof vis);while(n--){scanf("%d", &x);vis[x] = true;}bool ok = true;for(int i = 0; i < M; ++i){if(vis[num[i].u] || vis[num[i].v]) continue;ok = false;break;}if(ok) printf("Yes\n");else printf("No\n");}return 0;
}
1135 Is It A Red-Black Tree(30 分
题意:给定一棵二叉搜索树的前序遍历序列,判断其是否为一棵红黑树。
分析:
1、红黑树是一棵平衡的二叉搜索树,满足以下条件:
(1)所有结点不是红色就是黑色;
(2)根结点是黑色;
(3)每一个为NULL的叶子结点是黑色;
(4)如果某结点是红色,其左右子结点都是黑色;
(5)对于每个结点,其到所有后代叶子结点经过的黑色结点数相同;
2、根据给定的前序遍历序列,结合二叉搜索树的定义可以建树。
3、递归检查条件4。
4、同理,递归统计左右子树的黑色结点数,来检查条件5。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<string>
#include<algorithm>
#include<map>
#include<iostream>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
const int MAXN = 30 + 10;
struct Node{Node *left, *right;int value;
};
struct Node *root;
bool ok;
void build(Node* &r, int x){if(r == NULL){r = (Node*)malloc(sizeof(Node));r -> value = x;r -> left = r -> right = NULL;return;}if(abs(x) < abs(r -> value)){build(r -> left, x);}else{build(r -> right, x);}
}
void judge_RedNode(Node* r){if(!ok) return;if(r -> left != NULL){if(r -> value < 0 && r -> left -> value < 0){ok = false;return;}else judge_RedNode(r -> left);}if(r -> right != NULL){if(r -> value < 0 && r -> right -> value < 0){ok = false;return;}else judge_RedNode(r -> right);}
}
int judge_BlackNode(Node* r){int leftcnt, rightcnt;if(!ok) return -1;if(r == NULL) return 1;leftcnt = judge_BlackNode(r -> left);rightcnt = judge_BlackNode(r -> right);if(leftcnt != rightcnt){ok = false;return -1;}else{if(r -> value > 0) ++leftcnt;}return leftcnt;
}
int main(){int K;scanf("%d", &K);while(K--){root = NULL;int N, x;scanf("%d", &N);while(N--){scanf("%d", &x);build(root, x);}ok = true;judge_RedNode(root);judge_BlackNode(root);if(root -> value < 0 || !ok){printf("No\n");}else{printf("Yes\n");}}return 0;
}
转载于:https://www.cnblogs.com/tyty-Somnuspoppy/p/9560759.html
PAT (Advanced Level) 1132~1135:1132 模拟 1133模拟(易超时!) 1134图 1135红黑树相关推荐
- PAT (Advanced Level) Practice 题解代码 - II (1051-1100)
PAT PAT (Advanced Level) Practice - II(1051-1100) -------------------------------------------------- ...
- PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642
PAT (Advanced Level) Practice 1043 Is It a Binary Search Tree (25 分) 凌宸1642 题目描述: A Binary Search Tr ...
- PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642
PAT (Advanced Level) Practice 1011 World Cup Betting (20 分) 凌宸1642 题目描述: With the 2010 FIFA World Cu ...
- PAT (Advanced Level) 1140~1143:1140模拟 1141模拟 1142暴力 1143 BST+LCA
1140 Look-and-say Sequence(20 分) 题意:观察序列D, D1, D111, D113, D11231, D112213111, ...,显然后一个串是对前一个串每一小段连 ...
- PAT (Advanced Level) 1017 Queueing at Bank(模拟)
题目链接:点击查看 题目大意:模拟银行服务的过程,输出每个客户的平均等待时间 题目分析:类似的银行服务模拟题,不过与之前那个题不太一样的是,这一次所需要统计的信息变少了,只需要统计一下每个客户的平均等 ...
- PAT (Advanced Level) 1014 Waiting in Line(模拟)
题目链接:点击查看 题目大意:给出规则,要求模拟客户到银行办理手续的过程:为了方便描述,下面将分为等待区和服务区来称呼 银行共有n个窗口,每个窗口最多可以有m个人排队,这里我们称为服务区 若窗口排队人 ...
- PAT (Advanced Level) 1010 Radix(二分+模拟)
题目链接:点击查看 题目大意:给出两个数n1和n2,再给出其中一个数的进制,问另一个数能否选择一个进制,使得两个数的值相等 题目分析:首先这个题目一开始会错意了,因为题中的表示只给出了0~9以及a~z ...
- PAT (Advanced Level) 1016 Phone Bills(恶心模拟)
题目链接:点击查看 题目大意:模拟电话收费规则: 每个时间段的收费不同,时间段分为:00:00-01:00,01:00-02:00诸如此类 最开始给出的单价是每分钟的单价 最后输出每个用户的电话费 题 ...
- PAT (Advanced Level) Practice 题目集合(1001 ~ 1050)(正在更新)
1001 A+B Format (20 分) 题目大意:计算a+b,结果按照西方的那种写数字的方式输出,从三个数一个逗号那种. #include<bits/stdc++.h> using ...
最新文章
- 20 位百万富翁希望自己 20 岁就明白的事
- 思科认证与华为认证在考题与内容上到底多大差别?
- JDK的环境变量配置
- 学习CSS的背景图像属性background
- HTML 学习笔记3
- Dubbo使用启动时检查 check=“true“
- Oracle 数据库自动诊断库 ADR(Automatic Diagnostic Repository)简介
- [设计模式-行为型]状态模式(State)
- 聊聊网络安全行业这十年(2010-2019)
- VC字符处理(二)转换(修改)
- [RK3399][Android7.1] 基于regmap的I2C实现方法
- MySQL 报错:Translating SQLException with SQL state '42000', error code '1064', message
- swagger注解 详细说明
- pycharm专业版账号登录问题
- 史上最全的前端资源汇总(上)
- 程序员博客 - 加分项
- EMBA必看书籍推荐
- 低代码接口开发平台——YesApi(免费注册)
- react+vite+ts关于路径别名的配置
- AutoCAD 描图方法小结
热门文章
- 粒子群算法(1)----粒子群简要
- 构造 Codeforces Round #302 (Div. 2) B Sea and Islands
- Hutool之集合工具——CollectionUtil
- jQuery EasyUI API 中文文档 - DataGrid 数据表格
- Coolpad F61刷机解锁成功
- 网友为对百合所唱的最后的挽歌!(节选)
- HTTP协议中的Range和Content-Range
- 裸奔的支付X聊天,你还敢用吗?
- IDEA新建springboot项目发生错误
- (C++)小明种苹果(续)