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;
}

  

PAT (Advanced Level) 1132~1135:1132 模拟 1133模拟(易超时!) 1134图 1135红黑树的更多相关文章

  1. PAT (Advanced Level) Practice(更新中)

    Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...

  2. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  3. PAT (Advanced Level) Practice 1001-1005

    PAT (Advanced Level) Practice 1001-1005 PAT 计算机程序设计能力考试 甲级 练习题 题库:PTA拼题A官网 背景 这是浙大背景的一个计算机考试 刷刷题练练手 ...

  4. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  5. PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...

  6. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  7. PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...

  8. PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...

  9. PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...

  10. PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1019 General Palindromic Number (20 分) 凌宸1642 题目描述: A number that will ...

随机推荐

  1. 洛谷 P5057 [CQOI2006]简单题(树状数组)

    嗯... 题目链接:https://www.luogu.org/problem/P5057 首先发现这道题中只有0和1,所以肯定与二进制有关.然后发现这道题需要支持区间更改和单点查询操作,所以首先想到 ...

  2. mcast_get_if函数

    #include <errno.h> int sockfd_to_family(int); int mcast_get_if(int sockfd) { switch (sockfd_to ...

  3. vue.js 第八课

    列表渲染 v-for template v-for 数组变动检查 变异方法 替换数组 track-by track-by $index 问题 对象 v-for 值域 v-for 显示过滤/排序的结果 ...

  4. sshpass安装以及使用

    centos7如何安装sshpass 先安装epel yum install -y epel-release yum repolist 安装完成epel之后,就可以按照sshpass了 yum ins ...

  5. Python 基础之正则之一 单字符,多字符匹配及开头结尾匹配

    一.正则表达式之单个字符匹配 格式:lst = re.findall(正则表达式,要匹配的字符串)预定义字符集 匹配内容 .匹配任意字符,除了换行符\n \d匹配数字 \D匹配非数字 \w匹配字母或数 ...

  6. Struts笔记一

    Struts 概念: 是一个MVC框架: Servlet的缺点 1.在web.xml中文件中需要配置很多行代码,维护起来很不方便呢,不利于团队合作. 2.一个servlet的入口只有一个doPost或 ...

  7. HDU 5565:Clarke and baton

    Clarke and baton  Accepts: 14  Submissions: 79  Time Limit: 12000/6000 MS (Java/Others)  Memory Limi ...

  8. nacos集群配置

    一.    环境准备 Nacos 依赖 java环境来运行.如果您是从代码开始构建并运行Nacos,还需要为此配置 Maven环境,请确保是在以下版本环境中安装使用: 64 bit OS,支持 Lin ...

  9. JavaScript数组用法

    本文介绍一些js数组的用法: 上图的要点为: 1.unshift增加数组头部的元素,shift删除数组头部的元素. 2.delete除可删除对象的属性外,还可以删除数组的元素,使其占位变为undefi ...

  10. 2019年的代码都写完了吗?不如做个Python进度条看看还剩多少

    我们都知道,进度条是用来直观展示流程所需时间的优秀工具,以免我们担心流程会突然挂掉,而且我们可以用它来预测代码运行是否正常,借助进度条,每个人都能直观地看到脚本最新的进展情况. 如果你之前没用过进度条 ...