PAT (Advanced Level) 1132~1135:1132 模拟 1133模拟(易超时!) 1134图 1135红黑树

news/2024/7/5 1:41:33
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


http://lihuaxi.xjx100.cn/news/254285.html

相关文章

侠客X官方网站成立,第一个内测版本即将放出,敬请期待.

这是一个难忘的日子&#xff0c;西方的情人节&#xff0c;本站的成立代表侠客X&#xff0c;即将与大家见面了。 我们的要做的是&#xff0c;传承侠客站群经典模式&#xff0c;打造SEO王者力作&#xff0c;侠客X即将公开测试&#xff0c;敬请期待。 http://xpk.in Qin 转载于:ht…

Numpy入门教程:05. 逻辑函数

背景 什么是 NumPy 呢&#xff1f; NumPy 这个词来源于两个单词 – Numerical和Python。其是一个功能强大的 Python 库&#xff0c;可以帮助程序员轻松地进行数值计算&#xff0c;通常应用于以下场景&#xff1a; 执行各种数学任务&#xff0c;如&#xff1a;数值积分、微分、…

[工具推荐]用了TrueCrypt 再无难掩之隐

缘起&#xff1a;混在网络n多年了&#xff0c;手头总有些东西不想被别人看到的东西&#xff0c;由于小弟人品好&#xff0c;相貌佳&#xff0c;总有很多朋友喜欢用我的电脑玩啊玩啊……。 近日&#xff0c;冠希、柏芝等前辈以身示法&#xff0c;为我等上了很好一堂关于隐私保护…

WTF!只需一行 Python 代码即可玩 20 几款小游戏

作者 | Python小二来源 | Python小二今天分享一个有趣的 github 项目&#xff1a;https://github.com/kingser/free-python-games&#xff0c;通过该项目&#xff0c;我们只需一行代码即可玩 20 几款小游戏&#xff0c;下面具体来看一下。安装首先&#xff0c;我们进行安装&…

ARM体系结构

工作模式_ufisaus USRFRQIRQSVCABTUNDSYSR0R1R2R3R4R5R6R7R8R8_FRQR9R9_FRQR10R10_FRQR11R11_FRQR12R12_FRQSPSP_FRQSP_IRQSP_SVCSP_ABTSP_UNDLRLR_FRQLR_IRQLR_SVCLR_ABTLR_UNDPCCPSRSPSR_FRQSPSR_IRQSPSR_SVCSPSR_UNDSPSR_ABTUSR(User) &#xff1a;正常程序的执行状态FIQ(Fa…

Spring中毒太深,离开Spring居然连最基本的接口都不会写了

欢迎关注方志朋的博客&#xff0c;回复”666“获面试宝典来源&#xff1a;cnblogs.com/lonely-wolf/p/14127957.html前言Spring 能帮我们做什么控制反转&#xff08;IOC&#xff09;依赖注入&#xff08;DI&#xff09;面向切面编程&#xff08;AOP&#xff09;利用 Spring 来完…

Datawhale x OpenMMLab:共建国际一流开源项目!

CV 技术盛事超级视客营你的热门活动小助手已上线百万算力支持&#xff0c;100 实战项目任你挑选&#xff01;欢迎来到由 OpenMMLab 联合北京超级云计算中心主办、Datawhale 社区及上海市人工智能行业协会协办的计算机视觉任务实战活动——【超级视客营】第一期。我们诚邀全球开…

php获取输入流

uc中的用到的代码(在api/uc.php)代码&#xff1a; $post xml_unserialize(file_get_contents(php://input));&#xfeff; php手册&#xff08;http://cn.php.net/manual/zh/wrappers.php.php&#xff09;说明: php://input allows you to read raw data from the request bod…