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. uniGUI之UniSyntaxEdit(24)

    UniSyntaxEdit1语法高亮显示控件,主要属性Language,它是  多行 1]Language 语言 2]执行 FDquery1.Open(UniSyntaxEdit1.Lines.Tex ...

  2. jackson处理日期异常

    原 jackson处理日期异常 2018年01月09日 10:50:19 阅读数:70 1.异常信息 2.原因 默认情况下,fasterxml json只支持几种format,但是肯定不支持" ...

  3. 不高兴的津津(0)<P2004_1>

    不高兴的津津(unhappy.pas/c/cpp) [问题描述]  津津上初中了.妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班.另外每周妈妈还会送她去学习朗诵. ...

  4. Mysql 分组查询出现'this is incompatible with sql_mode=only_full_group_by'的解决办法

    由于Mysql自动开启了 only_full_group_by,所以若查询的字段不在group by里面,则分组报错. 解决办法其一:mysql配置,关闭only_full_group_by,这种办法 ...

  5. java 多线程小记

    /*java里面实现多线程一般有两种方式 * 1,继承Thread类,实现run方法 * 2,实现Runnable接口,实现run方法 * start方法可以新建一个线程,创建多线程必须调用此方法 * ...

  6. Struts笔记二:栈值的内存区域及标签和拦截器

    值栈和ognl表达式 1.只要是一个MVC框架,必须解决数据的存和取的问题 2.struts2利用值栈来存数据,所以值栈是一个存储数据的内存结构 1.  ValueStack是一个接口,在struts ...

  7. nginx 反向代理memcached、websocket及nginx文件方面的优化

    安装memcached服务,并启动添加数据 yum -y install memcached systemctl start memcached.service 启动 [root@python ~]# ...

  8. 吴裕雄--天生自然ORACLE数据库学习笔记:Oracle 11g的闪回技术

    alter system set db_recovery_file_dest_size=4g scope=both; connect system/1qaz2wsx as sysdba; archiv ...

  9. day11 作业

    # 1.编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件), # 要求登录成功一次,后续的函数都无需再输入用户名和密码 # FLAG = False # def login(func): ...

  10. Python学习笔记004

    变量 变量的命名规则1. 要具有描述性2. 变量名只能_,数字,字母组成,不可以是空格或特殊字符(#?<.,¥$*!~)3. 不能以中文为变量名4. 不能以数字开头,下划线或者小写字母开头,驼峰 ...