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. 「JSOI2010」挖宝藏

    「JSOI2010」挖宝藏 传送门 由于题目中说道挖一个位置的前提是挖掉它上面的三个,以此类推可以发现,挖掉一个点就需要挖掉这个点往上的整个倒三角,那么也就会映射到 \(x\) 轴上的一段区间(可以发 ...

  2. kubernetes从入门到放弃(二)

    kubernetes对象之pod 1.pod的认识 Pod直译是豆荚,可以把容器想像成豆荚里的豆子,把一个或多个关系紧密的豆子包在一起就是豆荚(一个Pod).在Kubernetes中我们不会直接操作容 ...

  3. nginx的access的阶段的access模块、auth_basic模块、auth_request模块及satisfy指令介绍

    access 模块 示例从上向下匹配 location / { deny 192.168.1.1; allow 192.168.1.0/24; allow 10.1.1.0/16; allow 200 ...

  4. 在webView中的返回键

    在写webView中我们按一下返回键,退到上一个我们浏览的网页,到第一个页面时,按两下退出程序,且按一下时提示你在按一下退出程序 只要加上这个方法即可 public void onBackPresse ...

  5. Unity表面着色器

    表面着色器和之前无光照着色器不同,其中没有顶点着色器和片元着色器,而增加了光照函数: 接下写了一个求两个贴图的光照效果 两个贴图做插值运算: Shader "Custom/SurfaceSh ...

  6. ApacheDbUtilsUpdate

    ApacheDbUtilsUpdate package p1; import com.DataSourceUtil; import org.apache.commons.dbutils.QueryRu ...

  7. wikipedia

    1. 维基百科 2. 更多维基项目 3. 有关维基百科的电影列表 4. 维基软件 5. 维基百科相关列表 6. 其他知识分享列表 7. 补充:维基百科使用中好用的关键字 1. 维基百科 https:/ ...

  8. Navigation源码(一) move_base最全解析

    一.概述 目测是全网最全的解析,花了几个小时通读并整理的,供大家参考学习. 概况的话可以看下古月居 https://www.guyuehome.com/270,其实它是翻译官方的,英语ok的可以去ro ...

  9. 学习SpringMVC 文件上传 遇到的问题,403:returned a response status of 403 Forbidden ,409文件夹未找到

    问题一: 409:文件夹没有创建好,找不到指定的文件夹,创建了即可. 问题二: 403:returned a response status of 403 Forbidden 我出现这个错误的原因是因 ...

  10. 「AT2021」キャンディーとN人の子供 / Children and Candies

    前言 今天练习赛出了这道题,由于我太菜没有在考场上做出来. 翻了题解后,感觉题解讲的并不是十分直观,所以自己写一篇. 题目大意 太长了,不讲了. 数据范围: \(1\leq N\leq 400\) \ ...