PAT考试真题解析
[1132] Cut Integer (20)
数学模拟
题目
Cutting an integer means to cut a K digits lone integer Z into two integers of (K/2) digits long integers A and B. For example, after cutting Z = 167334, we have A = 167 and B = 334. It is interesting to see that Z can be devided by the product of A and B, as 167334 / (167 × 334) = 3. Given an integer Z, you are supposed to test if it is such an integer.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 20). Then N lines follow, each gives an integer Z (10 ≤ Z <
Output Specification:
For each case, print a single line Yes
if it is such a number, or No
if not.
注意点与解析
- 直接⽤int保存num的值,计算出num的⻓度len,则令
d = pow(10, len / 2)
时,num取余d能得到后半部分的整数,num除以d能得到前半部分的整数,计算num % (a * b)
是否等于0就可以得知是否可以被整除. a * b
如果为0的时候不能取余,否则会浮点错误
代码
1 | int main() { |
[1133] Splitting A Linked List (25)
链表
题目
Given a singly linked list, you are supposed to rearrange its elements so that all the negative values appear before all of the non-negatives, and all the values in [0, K] appear before all those greater than K. The order of the elements inside each class must not be changed. For example, given the list being 18→7→-4→0→5→-6→10→11→-2 and K being 10, you must output -4→-6→-2→7→0→5→10→18→11.
Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive
Then
1 | Address Data Next |
where Address
is the position of the node, Data
is an integer in Next
is the position of the next node. It is guaranteed that the list is not empty.
Output Specification:
For each case, output in order (from beginning to the end of the list) the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.
代码
1 | const int maxn = 100005; |
[1134] Vertex Cover (25)
图论、哈希
题目
A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at least one vertex of the set. Now given a graph with several vertex sets, you are supposed to tell if each of them is a vertex cover or not.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers
After the graph, a positive integer
where
Output Specification:
For each query, print in a line Yes
if the set is a vertex cover, or No
if not.
注意点与解析
- 每条边与顶点覆盖集合中的至少一个顶点”incident”是指顶点覆盖集合中的顶点与图中的边直接相连或具有直接的入边或出边关系。
- 顶点覆盖是指在一个无向图中选择一些顶点,使得图中的每条边至少有一个端点被选中。
- ⽤
vector v[n]
保存某结点属于的某条边的编号,⽐如a、b两个结点构成的这条边的编号为0,则v[a].push_back(0)
,v[b].push_back(0)
——表示a属于0号边,b也属于0号边。对于每⼀个集合做判断,遍历集合中的每⼀个元素,将当前元素能够属于的边的编号i
对应的hash[i]
标记为1,表示这条边是满⾜有⼀个结点出⾃集合S中的。最后判断hash
数组中的每⼀个值是否都是1,如果有不是1的,说明这条边的两端结点没有⼀个出⾃集合S中.
代码
1 | int main() { |
[1135] Is It A Red-Black Tree (30)
二叉搜索树
题目
There is a kind of balanced binary search tree named red-black tree in the data structure. It has the following 5 properties:
- (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.
For example, the tree in Figure 1 is a red-black tree, while the ones in Figure 2 and 3 are not.
For each given binary search tree, you are supposed to tell if it is a legal red-black tree.
Input Specification:
Each input file contains several test cases. The first line gives a positive integer K (≤30) which is the total number of cases. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the preorder traversal sequence of the tree. While all the keys in a tree are positive integers, we use negative signs to represent red nodes. All the numbers in a line are separated by a space. The sample input cases correspond to the trees shown in Figure 1, 2 and 3.
Output Specification:
For each test case, print in a line “Yes” if the given tree is a red-black tree, or “No” if not.
代码
1 | struct TreeNode { |