1059. C语言竞赛(20)-PAT乙级真题

C语言竞赛是浙江大学计算机学院主持的一个欢乐的竞赛。既然竞赛主旨是为了好玩,颁奖规则也就制定得很滑稽:

0. 冠军将赢得一份“神秘大奖”(比如很巨大的一本学生研究论文集……)。
1. 排名为素数的学生将赢得最好的奖品 —— 小黄人玩偶!
2. 其他人将得到巧克力。

给定比赛的最终排名以及一系列参赛者的ID,你要给出这些参赛者应该获得的奖品。

输入格式:

输入第一行给出一个正整数N(<=10000),是参赛者人数。随后N行给出最终排名,每行按排名顺序给出一位参赛者的ID(4位数字组成)。接下来给出一个正整数K以及K个需要查询的ID。

输出格式:

对每个要查询的ID,在一行中输出“ID: 奖品”,其中奖品或者是“Mystery Award”(神秘大奖)、或者是“Minion”(小黄人)、或者是“Chocolate”(巧克力)。如果所查ID根本不在排名里,打印“Are you kidding?”(耍我呢?)。如果该ID已经查过了(即奖品已经领过了),打印“ID: Checked”(不能多吃多占)。

输入样例:
6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222
输出样例:
8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

分析:ran数组标记每个id对应的排名,集合ss存储所有已经询问过的id,如果发现当前id已经出现在ss中,则输出“Checked”,如果ran[id] == 0说明当前id不在排名列表中,所以输出“Are you kidding?”,如果ran[id]为1则输出“Minion”,如果ran[id]为素数则输出“Mystery Award”,否则输出“Chocolate”

 

1119. Pre- and Post-order Traversals (30)-PAT甲级真题(前序后序转中序)

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

Input Specification:

Each input file contains one test case. 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 sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line “Yes” if the tree is unique, or “No” if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input 1:
7
1 2 3 4 6 7 5
2 6 7 4 5 3 1
Sample Output 1:
Yes
2 1 6 4 7 3 5
Sample Input 2:
4
1 2 3 4
2 4 3 1
Sample Output 2:
No
2 1 3 4

题目大意:给出一棵树的结点个数n,以及它的前序遍历和后序遍历,输出它的中序遍历,如果中序遍历不唯一就输出No,且输出其中一个中序即可,如果中序遍历唯一就输出Yes,并输出它的中序
分析:用unique标记是否唯一,如果为true就表示中序是唯一的~
已知二叉树的前序和后序是无法唯一确定一颗二叉树的,因为可能会存在多种情况,这种情况就是一个结点可能是根的左孩子也有可能是根的右孩子,如果发现了一个无法确定的状态,置unique = false,又因为题目只需要输出一个方案,可以假定这个不可确定的孩子的状态是右孩子,接下来的问题是如何求根结点和左右孩子划分的问题了,首先我们需要知道树的表示范围,需要四个变量,分别是前序的开始的地方prel,前序结束的地方prer,后序开始的地方postl,后序结束的地方postr,前序的开始的第一个应该是后序的最后一个是相等的,这个结点就是根结点,以后序的根结点的前面一个结点作为参考,寻找这个结点在前序的位置,就可以根据这个位置来划分左右孩子,递归处理~

 

1118. Birds in Forest (25)-PAT甲级真题(并查集)

Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in the same picture belong to the same tree. You are supposed to help the scientists to count the maximum number of trees in the forest, and for any pair of birds, tell if they are on the same tree.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (<= 104) which is the number of pictures. Then N lines follow, each describes a picture in the format:
K B1 B2 … BK
where K is the number of birds in this picture, and Bi’s are the indices of birds. It is guaranteed that the birds in all the pictures are numbered continuously from 1 to some number that is no more than 104.

After the pictures there is a positive number Q (<= 104) which is the number of queries. Then Q lines follow, each contains the indices of two birds.

Output Specification:

For each test case, first output in a line the maximum possible number of trees and the number of birds. Then for each query, print in a line “Yes” if the two birds belong to the same tree, or “No” if not.

Sample Input:
4
3 10 1 2
2 3 4
4 1 5 7 8
3 9 6 4
2
10 5
3 7
Sample Output:
2 10
Yes
No

题目大意:一幅画里面的鸟为同一棵树上的,问有多少棵树和多少只鸟,以及对于两只鸟判断是否在同一个树上~

分析:使用并查集就可以啦~将一幅画中鸟使用Union函数合并在同一个集合里面,cnt[i]数组保存以i为FindFather结点的集合里面的鸟的个数,exist[i]表示鸟的id——i在输入的鸟的序号里面是否出现过,遍历cnt数组并累加所有不为0的个数即可得知有多少棵树,累加所有出现过的鸟的id的cnt的值即可得知鸟的个数~判断两只鸟是否在同一棵树,只需要知道这两只鸟的findFather是否相等即可~

1060. 爱丁顿数(25)-PAT乙级真题

英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数”E,即满足有E天骑车超过E英里的最大整数E。据说爱丁顿自己的E等于87。

现给定某人N天的骑车距离,请你算出对应的爱丁顿数E(<=N)。

输入格式:

输入第一行给出一个正整数N(<=105),即连续骑车的天数;第二行给出N个非负整数,代表每天的骑车距离。

输出格式:

在一行中给出N天的爱丁顿数。

输入样例:
10
6 7 6 9 3 10 8 2 7 8
输出样例:
6

分析:从下标1开始存储n天的公里数在数组a中,对n个数据从大到小排序,i表示了骑车的天数,那么满足a[i] > i的最大值即为所求~

 

1117. Eddington Number(25)-PAT甲级真题

British astronomer Eddington liked to ride a bike. It is said that in order to show off his skill, he has even defined an “Eddington number”, E — that is, the maximum integer E such that it is for E days that one rides more than E miles. Eddington’s own E was 87.

Now given everyday’s distances that one rides for N days, you are supposed to find the corresponding E (<=N).

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N(<=105), the days of continuous riding. Then N non-negative integers are given in the next line, being the riding distances of everyday.

Output Specification:

For each case, print in a line the Eddington number for these N days.

Sample Input:
10
6 7 6 9 3 10 8 2 7 8
Sample Output:
6

题目大意:英国天文学家爱丁顿很喜欢骑车。据说他为了炫耀自己的骑车功力,还定义了一个“爱丁顿数”E,即满足有E天骑车超过E英里的最大整数E。据说爱丁顿自己的E等于87。
分析:在数组a中存储n天的公里数,对n个数据从大到小排序,根据排序后的数组可得知骑车大于等于a[i] 英里的天数有 i+1 天,因为题目中要求超过E英里且所有的英里数为整数,所以可得知骑车超过a[i]-1 英里的天数有 i+1 天,如样例,从大到小排序后结果为10、9、8、8、7、7、6、6、3、2,骑车超过9英里的有1天,超过8英里的有2天,超过7英里的有4天/5天,超过6英里的有5天/6天,超过5英里的有7天/8天…英里数不断减少,天数不断增加,既然已知公里数和天数对应的关系,要想满足题意,必须使超过的英里数a[i]-1大于等于天数i+1(比如样例中,超过6英里的有6天,超过5英里的有7天,前者的天数6满足题意这6天都超过了6英里,后者的天数7不满足因为这7天只满足骑车都超过5英里)即a[i]-1 >= i+1,也就是 a[i]  >= i + 2,也就是a[i] > i + 1~从头到尾遍历数组,那么满足a[i] > i+1的最大i+1即为所求~

1116. Come on! Let’s C (20)-PAT甲级真题

“Let’s C” is a popular and fun programming contest hosted by the College of Computer Science and Technology, Zhejiang University. Since the idea of the contest is for fun, the award rules are funny as the following:

0. The Champion will receive a “Mystery Award” (such as a BIG collection of students’ research papers…).
1. Those who ranked as a prime number will receive the best award — the Minions (小黄人)!
2. Everyone else will receive chocolates.

Given the final ranklist and a sequence of contestant ID’s, you are supposed to tell the corresponding awards.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=10000), the total number of contestants. Then N lines of the ranklist follow, each in order gives a contestant’s ID (a 4-digit number). After the ranklist, there is a positive integer K followed by K query ID’s.

Output Specification:

For each query, print in a line “ID: award” where the award is “Mystery Award”, or “Minion”, or “Chocolate”. If the ID is not in the ranklist, print “Are you kidding?” instead. If the ID has been checked before, print “ID: Checked”.

Sample Input:
6
1111
6666
8888
1234
5555
0001
6
8888
0001
1111
2222
8888
2222
Sample Output:
8888: Minion
0001: Chocolate
1111: Mystery Award
2222: Are you kidding?
8888: Checked
2222: Are you kidding?

分析:ran数组标记每个id对应的排名,集合ss存储所有已经询问过的id,如果发现当前id已经出现在ss中,则输出“Checked”,如果ran[id] == 0说明当前id不在排名列表中,所以输出“Are you kidding?”,如果ran[id]为1则输出“Minion”,如果ran[id]为素数则输出“Mystery Award”,否则输出“Chocolate”