昨天去考pat,居然四题都一次ac了~~小激动,之前自己模拟测都是 八九十,有些测试用例死活想不到错误原因。

第一题 1132. Cut Integer,简单题。测试的时候想到了除零的情况。

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <cmath>using namespace std;
/*
3
167334
2333
123456783
10
2333
12345678
*/
void split( int num, int &a, int &b)
{int digits=0, n=num;while( n > 0 ){digits++;n /= 10;}int mod = pow(10, digits/2);a = num%mod;b = num/mod;//printf("digit = %d %d %d\n", digits, a, b);
}
int main()
{int n;scanf("%d", &n);int num, a,b;for (int i=0; i<n; i++){scanf("%d", &num);split( num, a, b);if ( a==0 || b==0 )printf("No\n");else if ( num % a !=0 ) printf("No\n");else{num /= a;if( num %b != 0) printf("No\n");else           printf("Yes\n");}}return 0;
}

第二题 1133. Splitting A Linked List  链表的操作题。题目输入已经定好了地址,杜绝了直接用stl的list。 地址范围不大,所以直接用二维数组实现链表。

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <cmath>using namespace std;
#define maxn 100001
int head,n,k;
int List[maxn][2];void print( int n)
{while( n != -1){printf("%05d %d ", n, List[n][0], List[n][1] );if (List[n][1] == -1)printf("-1\n");elseprintf("%05d\n", List[n][1]);n = List[n][1];}//printf("NULL\n");
}
int main()
{scanf("%d %d %d", &head, &n, &k);int a,d,next;for (int i=0; i<n; i++){scanf("%d %d %d", &a, &d, &next);List[a][0] = d;List[a][1] = next;}//找负数int *cur = &head, newList = -1, *newTail=&newList;while( *cur >= 0 ) //未到达链表尾 {//printf("====%d\n", List[*cur][0]);if( List[*cur][0] < 0 ){int tmp = *cur;*cur = List[tmp][1]; // 拆下节点tmp, 续上链表 List[tmp][1] = *newTail;*newTail = tmp; //节点tmp 接到新链表 newTail = &List[tmp][1];}elsecur = &List[*cur][1];}//找<=k的cur = &head; //从头再找 while( *cur >= 0 ) //未到达链表尾 {//printf("====%d\n", List[*cur][0]);if( List[*cur][0] <= k ){int tmp = *cur;*cur = List[tmp][1]; // 拆下节点tmp, 续上链表 List[tmp][1] = *newTail;*newTail = tmp; //节点tmp 接到新链表 newTail = &List[tmp][1];}elsecur = &List[*cur][1];}// 剩下的拼上去 *newTail = head; print(newList);return 0;
}

第三题  1134. Vertex Cover 读题有点磕磕碰碰 is incident to 不理解是什么意思。只能根据样例连猜带蒙。

大意就是给一个图和若干个查询点集。

对每个点集里面的点是否和图里面的所有边都沾边。 也就是图的任一边的必须有端点在点集内。

所以思路就出来了,计算点集内的每个节点 V, 从V 出发能有多少边。去重之后加在一起的边数等于整个图的边数,就是Yes。否则No。

 #include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <vector>
#include <algorithm>using namespace std;
int n,m;//节点数, 边数
vector<short> g[10001]; //short 足够表示 32768
bool used[10001];  // 查询时,判断这个节点是否用过了int caledge( const vector<short>& q)
{int sum = 0, v;for( int i=0; i<q.size(); i++){v = q[i];for(int w=0; w<g[v].size(); w++ ){if ( used[ g[v][w] ] == false ) ++sum;}used[ v ] = true;//printf("q=%d sum=%d\n", v, sum);}return sum;
}int main()
{scanf("%d %d", &n, &m);short a,b, tmp;for (int i=0; i<m; i++) //建立图 {scanf("%hd %hd", &a, &b);g[a].push_back(b);g[b].push_back(a);}scanf("%d", &a); //a个查询vector<short> qvex; //待查询点集 for(int i=0; i<a; i++){scanf("%hd", &b); //点集大小for( int j=0; j<b; j++){scanf("%hd", &tmp);qvex.push_back(tmp);}if (caledge( qvex ) == m ){printf("Yes\n");}else{printf("No\n");}qvex.clear();memset(used, 0, sizeof(used) ); } return 0;
}/*10 11
8 7
6 8
4 5
8 4
8 1
1 2
1 4
9 8
9 1
1 0
2 4
5
4 0 3 8 4
6 6 1 7 5 4 9
3 1 8 4
2 2 8
7 9 8 7 6 5 4 2*/

第四题 1135. Is It A Red-Black Tree 这题是标题党,一开始吓了一跳,居然考了红黑树,完全不会啊怎么办。

后来看了题目,发现只是判断输入的二叉搜索树是不是满足红黑的属性。

根据BST的先序还原出BST的树结构以及每个节点的红黑属性。

再来就是按题目说明的红黑树的条件逐条检查,看看是否完全符合。

(1) Every node is either red or black.
(2) The root is black.
(3) Every leaf (NULL) is black.
(4) If a node is red, then both its children are black.
(5) For each node, all simple paths from the node to descendant leaves contain the same number of black nodes.

条件一不用检查,本来我们的节点就只能要么红要么黑,还原出来的树肯定是符合的。

条件二三说明 红黑树一定是黑色叶节点的。毕竟null的节点也算黑色。

条件四就是红色节点不能相邻,递归遍历每个节点,检查红节点的两个儿子就好了。

条件五略复杂,从任一节点出发到他的后继叶子,不管走哪条路径,黑节点数目是相同的。想了想,和计算树高度有点像。只不过这里计算的是黑色高度

思路就来了,从根节点递归到叶子,递归返回时节点是黑色就加1,否则加0。注意叶节点一定是null,是黑色。

然后在逐层计算的时候发现有某一节点的左右子树的黑色高度不一致,就可以直接返回结果了不需要继续算。

#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cstring>
#include <cmath>using namespace std;
struct rbt{int d, isred;rbt *left,*right;rbt( int data=0, rbt * l=NULL, rbt * r=NULL){left = l, right=r;setdata(data);}void setdata(int data){isred = data<0;  //负数表示红色节点d     = data<0?-data:data;}
};
bool isred( rbt* p ) //NULL节点算黑色
{if( p )return p->isred;return 0;
}
// 区间 [l, h)
void buildtree(rbt*&subroot, int preorder[], int l, int h)
{if( l>=h ) return; subroot = new rbt(preorder[l]);int mid=l+1;for( ; mid<h; mid++){if ( abs(preorder[mid] ) > subroot->d ) break; //根据先序分割左右子树 }buildtree( subroot->left, preorder, l+1, mid );buildtree( subroot->right, preorder, mid, h );
}
void preShow( rbt*subroot )
{if ( subroot == NULL ) return ;printf("%d %d\n", subroot->d, subroot->isred);preShow( subroot->left );preShow( subroot->right );
}//检查红节点的两儿子
void checkRedChildren( rbt*subroot, bool&ret ){if ( ret == true )return ;if ( subroot == NULL ) return ;if ( isred(subroot) ) {if (isred(subroot->left) || isred(subroot->right) ){ret = true;return;}}checkRedChildren(subroot->left, ret);checkRedChildren(subroot->right, ret);
}//int checkBlackNum(rbt*subroot, bool&ret)
{if(ret == true)   return 1;if( subroot == NULL )return 1;int leftB, rightB, isBlack = !isred(subroot);leftB =  isBlack + checkBlackNum(subroot->left, ret);rightB = isBlack + checkBlackNum(subroot->right, ret);if (leftB != rightB){ret = true;}return leftB;
} bool checkRB( rbt*root )
{if ( root->isred )  return false; //根节点必须是黑的 bool ret=false;checkRedChildren(root, ret);if ( ret == true )  return false; //红节点有红儿子,不是红黑树 checkBlackNum (root, ret);if ( ret == true ) return false; //节点到叶子的黑色数目不等 return true;
}
int main()
{int k,n, t;scanf("%d", &k);while( k-- ){scanf("%d", &n);int preorder[n];for ( int i=0; i<n; i++){scanf("%d",preorder+i); //万一输入里面有0呢,要处理 }rbt* root=NULL;buildtree( root, preorder, 0, n );//    preShow(root);if( checkRB(root) ) printf("Yes\n");else  printf("No\n");}return 0;
}
/*
3
9
7 -2 1 5 -4 -11 8 14 -15
9
11 -2 1 -7 5 -4 8 14 -15
8
10 -7 5 -6 8 15 -11 17*//*
红色节点的相邻节点一定是黑色.
黑色的可以和黑色的相邻
*/

用时:

1:30开考,然后据说新的pat系统被限流了,导致拥堵。后来改到用浏览器提交,2:45正式开始~~

其实感觉挺好的,1:30正是困的时候,中间休息一会缓过来了,再敲个最小堆,矩阵乘法什么的热热身就进入状态了~~

第一题 15min

第二题 30min

第三题 40~50min

第四题 60+min

最后还有半小时结余~~撒花完结本日。

2017-9-17 PAT考试记相关推荐

  1. 高考2017c语言试卷,2017高考语文模拟考试试卷附答案

    2017高考语文的备考需要考生多做模拟考试试卷查漏补缺.接下来,学习啦小编为你分享2017高考语文模拟考试试卷,希望对你有帮助. 2017高考语文模拟考试试卷附答案一.阅读80分 (一)阅读下文,完成 ...

  2. 协税员计算机考试题,2017年计算机等级考试基础题试卷「附答案」

    2017年计算机等级考试基础题试卷「附答案」 一.单选题 1.信息可以通过声.图.文等信息传播媒体在空间传播是指信息的___C_____性. A.失效 B.可识别 C.传递 D.存储 2.激光唱盘对音 ...

  3. 2015中学计算机考试题,2017年初中信息技术考试试题及答案

    2017年初中信息技术考试试题及答案 1.frontpage中,网页文件的扩展名是______ b .html 2.硬盘工作时应特别注意避免 c 震动 3.在windows2000的"资源管 ...

  4. 全国计算机一级考试的练题软件,2017全国计算机一级考试WPS备考练习题

    2017全国计算机一级考试WPS备考练习题 计算机等级考试要取得好成绩平时一定要多加练习,提高做题技巧和速度.下面是小编为大家整理的2017全国计算机一级考试WPS备考练习题,希望对大家有帮助! 单选 ...

  5. 2017计算机三级哪个好考,快速突破2017年计算机三级考试的几大复习阶段

    原标题:快速突破2017年计算机三级考试的几大复习阶段 为某种原因,本人共考过2次笔试,3次上机.其中笔试在70左右,3次上机均为满分,看着大批的同学在为三级发愁不知道如何准备和应考.我想我应该把经验 ...

  6. 计算机应用 winxp,2017年职称计算机考试模块WindowsXP试题

    2017年职称计算机考试模块WindowsXP试题 全国专业技术人员计算机应用能力考试是专业技术人员资格考试的一种.接下来应届毕业生小编为大家搜索整理了2017年职称计算机考试模块WindowsXP试 ...

  7. j计算机一级考试题,2017全国计算机一级考试试题与答案

    2017全国计算机一级考试试题与答案 1.微型计算机硬件系统中最核心的部件是( ). 答案:B A.主板   B.CPU   C.内存储器   D.I/O设备 2.下列术语中,属于显示器性能指标的是( ...

  8. 2017年对口招生c语言及答案,2017年计算机专业对口考试试卷及答案.doc

    2017年计算机专业对口考试试卷及答案 2017年计算机专业对口考试试卷及答案 一.单项选择题 1.以下软件中,是系统软件. ..x.x. 2.计算机能直接识别的语言是. A.汇编语言 B.自然语言 ...

  9. 2017计算机湖北对口试题答案,2017年计算机专业对口考试试卷及答案

    2017年计算机专业对口考试试卷及答案 一.单项选择题(在每小题的四个备选答案中,选出一个正确答案,并将正确答案的序号填在题干的括号内.每小题2分,共80分) 1.以下软件中,( )是系统软件. A. ...

最新文章

  1. JavaScript基础笔记集合(转)
  2. html页面校园美景相框,纯CSS+HTML打造图片相框背景
  3. OceanBase开源,11张图带你了解分布式数据库的核心知识
  4. 【每日一题】7月9日题目 Color
  5. MyEclipse从数据库反向生成实体类之Hibernate方式 反向工程
  6. 谁是ASML的最大股东?为何荷兰光刻巨头要听美国的话?
  7. Luogu 1019 单词接龙
  8. 大端字节序与小端字节序的转换
  9. 磊科路由虚拟服务器设置,磊科路由器虚拟转发服务设置的方法
  10. NeurIPS2019无人驾驶研究成果大总结(含大量论文及项目数据)
  11. webbench接口并发测试
  12. easyui源码翻译1.32--Droppable(放置)
  13. Java中的浅克隆和深克隆
  14. idea为java文件自动生成copyright
  15. matlab 函数怎么写,MATLAB怎样定义函数(入门) 有一函数 f(x,y)=x^2+sinxy+2y , 写一程序, 输入自变量的值,输出函数值....
  16. 女孩取名:带日字旁好听有内涵的女孩名字
  17. 来看看怎样让你的VSR模型跑的和苏炳添一样快
  18. 活性氧Propiconazole-d3 (nitrate),CAS No. 2699607-26-4
  19. 深度学习入门之神经网络的学习
  20. 《云计算服务安全能力要求》与《云计算服务安全指南》标准

热门文章

  1. 【高等数学】多元函数微分法及其应用1
  2. 30个题型+代码(冲刺2023蓝桥杯)(中)
  3. swagger测试导出报URL.createObjectURL: Argument 1 is not valid for any of the 1-argument overloads.
  4. 【APP】怎么对App进行功能测试
  5. HTML 框架[frameiframe]
  6. 解决HTML框架不能显示的问题
  7. Blender 2.8快捷键
  8. 交叉熵三连(3)——交叉熵及其使用
  9. 能说会道爱办公——“别人家的”Chrome插件到底怎么做
  10. 笔记本电脑开不了机的原因以及解决方法