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”

 

1069. The Black Hole of Numbers (20)-PAT甲级真题

For any 4-digit integer except the ones with all the digits being the same, if we sort the digits in non-increasing order first, and then in non-decreasing order, a new number can be obtained by taking the second number from the first one. Repeat in this manner we will soon end up at the number 6174 — the “black hole” of 4-digit numbers. This number is named Kaprekar Constant.

For example, start from 6767, we’ll get:

7766 – 6677 = 1089
9810 – 0189 = 9621
9621 – 1269 = 8352
8532 – 2358 = 6174
7641 – 1467 = 6174
… …

Given any 4-digit number, you are supposed to illustrate the way it gets into the black hole.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range (0, 10000).

Output Specification:

If all the 4 digits of N are the same, print in one line the equation “N – N = 0000”. Else print each step of calculation in a line until 6174 comes out as the difference. All the numbers must be printed as 4-digit numbers.

Sample Input 1:
6767
Sample Output 1:
7766 – 6677 = 1089
9810 – 0189 = 9621
9621 – 1269 = 8352
8532 – 2358 = 6174
Sample Input 2:
2222
Sample Output 2:
2222 – 2222 = 0000

分析:有一个测试用例注意点,如果当输入N值为6174的时候,依旧要进行下面的步骤,直到差值为6174才可以~所以用do while语句,无论是什么值总是要执行一遍while语句,直到遇到差值是0000或6174~

s.insert(0, 4 – s.length(), ‘0’);用来给不足4位的时候前面补0~

1090. Highest Price in Supply Chain (25)-PAT甲级真题(DFS)

A supply chain is a network of retailers(零售商), distributors(经销商), and suppliers(供应商)– everyone involved in moving a product from supplier to customer.

Starting from one root supplier, everyone on the chain buys products from one’s supplier in a price P and sell or distribute them in a price that is r% higher than P. It is assumed that each member in the supply chain has exactly one supplier except the root supplier, and there is no supply cycle.

Now given a supply chain, you are supposed to tell the highest price we can expect from some retailers.

Input Specification:

Each input file contains one test case. For each case, The first line contains three positive numbers: N (<=105), the total number of the members in the supply chain (and hence they are numbered from 0 to N-1); P, the price given by the root supplier; and r, the percentage rate of price increment for each distributor or retailer. Then the next line contains N numbers, each number Si is the index of the supplier for the i-th member. Sroot for the root supplier is defined to be -1. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the highest price we can expect from some retailers, accurate up to 2 decimal places, and the number of retailers that sell at the highest price. There must be one space between the two numbers. It is guaranteed that the price will not exceed 1010.

Sample Input:
9 1.80 1.00
1 5 4 4 -1 4 5 3 6
Sample Output:
1.85 2

题目大意:给一棵树,在树根处货物的价格为p,然后每往下走一层,价格增加r%,求所有叶子结点的最高价格,以及这个价格的叶子结点个数。
分析:用二维数组v[i][j]存储,对于每一个结点i,它的孩子结点的下标push_back存储在v[i]中。用深度优先搜索dfs,保存当前结点的下标index以及当前结点所在层数,当 当前结点的孩子结点个数为0的时候说明是叶子结点,更新maxdepth和maxnum的值,最后输出~
注意:如果采用保存某个结点的父结点的下标的形式,然后一直遍历到根结点的深度/广度优先,会出现三个超时。因为从叶子结点往上遍历将会把所有路径都走一遍,很多都是重复走的路径,会超时,没有从根结点往下遍历的方式快~~
记得r是百分比,要除以100之后再计算复利~

 

int输出%f浮点值是0,double/float 浮点数%d输出0的原因

输出为:

  • int输出%f浮点值的时候,比如2,2内部表示如果看作是float,是个很小的数所以输出的是0.000000
  • double/float 浮点数按照%d输出,如果输出为0,则说明a的数据放在地址的高端,而整型比浮点数内存中占的字节数少,整型只会把属于它的字节数读出来,如在Win32,VC6.0下,Int是4位,它就会把从a开始的4位读出来(按整型格式),所以它把浮点数低端地址的0给输出出来。
  • 所以说使用printf的时候数据格式一定要对应,或者使用printf("%d", (int)a);这样强制转换的方法输出不同格式的数据

1033. To Fill or Not to Fill (25)-PAT甲级真题(贪心算法)

With highways available, driving a car from Hangzhou to any other city is easy. But since the tank capacity of a car is limited, we have to find gas stations on the way from time to time. Different gas station may give different price. You are asked to carefully design the cheapest route to go.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 positive numbers: Cmax (<= 100), the maximum capacity of the tank; D (<=30000), the distance between Hangzhou and the destination city; Davg (<=20), the average distance per unit gas that the car can run; and N (<= 500), the total number of gas stations. Then N lines follow, each contains a pair of non-negative numbers: Pi, the unit gas price, and Di (<=D), the distance between this station and Hangzhou, for i=1,…N. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the cheapest price in a line, accurate up to 2 decimal places. It is assumed that the tank is empty at the beginning. If it is impossible to reach the destination, print “The maximum travel distance = X” where X is the maximum possible distance the car can run, accurate up to 2 decimal places.

Sample Input 1:
50 1300 12 8
6.00 1250
7.00 600
7.00 150
7.10 0
7.20 200
7.50 400
7.30 1000
6.85 300
Sample Output 1:
749.17
Sample Input 2:
50 1300 12 2
7.10 0
7.00 600
Sample Output 2:
The maximum travel distance = 1200.00

题目大意:汽车从杭州出发可以通过高速公路去任何城市,但是油箱的容量是有限的,路上有很多加油站,每个加油站的价格不同,为汽车设计一个从杭州到终点的最便宜的路线,Cmax表示油箱最大容量,D表示杭州到目的地的距离,Davg表示平均每单位的汽油可以让汽车行驶的距离,N表示汽车的站点数量,每个站点都会给出它的单位油价Pi和汽车站点和杭州的距离Di,求汽车从杭州到终点的最小花费,如果不能够到达,就输出汽车能够行驶的最大距离

分析:贪心算法。
0.假设增加一个目的地处的加油站,距离为目的地的距离,价格为0,考虑从0距离开始能否到达最后一个加油站的问题
1.因为先开始没有油,所以如果所有的加油站距离都没有等于0的,那么说明车哪也去不了,直接输出并return
2.将加油站按照距离dis从小到大排序
3.先去第一个加油站,设置变量nowdis表示当前所在的距离,maxdis是能够到达的最大距离,nowprice是当前的站点的价格,totalPrice是总的价格。
贪心思想:
0.寻找比自己距离远的,到能够到达的最大距离之间的加油站,看他们的油价。如果找到了更低价格的油价,就加油到刚好能到达那个加油站的距离的油,然后去那个更低价格的加油站(有更低的我一分都不想多花在别的距离上,只加到刚好满足更低价格的加油站的距离就行,那样以后的路程我就可以以更低的价格行驶啦)
1.如果找不到更低的,就找尽可能低的油价的加油站,在当前加油站加满油之后过去。因为想要让路程上使用的尽可能是低价的油,既然没有比当前更低价格的了,就让油箱加到最大值,这样能保证利益最大化,保证最大的距离使用的是便宜的油。

 

1010. Radix (25)-PAT甲级真题(二分法)

Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The answer is “yes”, if 6 is a decimal number and 110 is a binary number.

Now for any pair of positive integers N1 and N2, your task is to find the radix of one number while that of the other is given.

Input Specification:

Each input file contains one test case. Each case occupies a line which contains 4 positive integers:
N1 N2 tag radix
Here N1 and N2 each has no more than 10 digits. A digit is less than its radix and is chosen from the set {0-9, a-z} where 0-9 represent the decimal numbers 0-9, and a-z represent the decimal numbers 10-35. The last number “radix” is the radix of N1 if “tag” is 1, or of N2 if “tag” is 2.

Output Specification:

For each test case, print in one line the radix of the other number so that the equation N1 = N2 is true. If the equation is impossible, print “Impossible”. If the solution is not unique, output the smallest possible radix.

Sample Input 1:
6 110 1 10
Sample Output 1:
2
Sample Input 2:
1 ab 1 2
Sample Output 2:
Impossible

分析:convert函数:给定一个数值和一个进制,将它转化为10进制。转化过程中可能产生溢出
find_radix函数:找到令两个数值相等的进制数。在查找的过程中,需要使用二分查找算法,如果使用当前进制转化得到数值比另一个大或者小于0,说明这个进制太大~