PAT甲级 1038 Recover the Smallest Number (30分)

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87. Input Specification: Each input file contains one test case. Each case gives a positive integer N (≤10^4) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space. ...

March 28, 2020 · 2 min

PAT甲级 1067 Sort with Swap(0, i) (25分)

Given any permutation of the numbers {0, 1, 2,…, N−1}, it is easy to sort them in increasing order. But what if Swap(0, *) is the ONLY operation that is allowed to use? For example, to sort {4, 0, 2, 1, 3} we may apply the swap operations in the following way: Swap(0, 1) => {4, 1, 2, 0, 3} Swap(0, 3) => {4, 1, 2, 3, 0} Swap(0, 4) => {0, 1, 2, 3, 4} Now you are asked to find the minimum number of swaps need to sort the given permutation of the first N nonnegative integers. ...

March 27, 2020 · 2 min

PAT乙级 1033 旧键盘打字 (20分)

旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现。现在给出应该输入的一段文字、以及坏掉的那些键,打出的结果文字会是怎样? 输入格式: 输入在 2 行中分别给出坏掉的那些键、以及应该输入的文字。其中对应英文字母的坏键以大写给出;每段文字是不超过 10^5 个字符的串。可用的字符包括字母 [a-z, A-Z]、数字 0-9、以及下划线 _(代表空格)、,、.、-、+(代表上档键)。题目保证第 2 行输入的文字串非空。 注意:如果上档键坏掉了,那么大写的英文字母无法被打出。 输出格式: 在一行中输出能够被打出的结果文字。如果没有一个字符能被打出,则输出空行。 输入样例: 7+IE. 7_This_is_a_test. 输出样例: _hs_s_a_tst 注意点 读入失效键位的时候要注意,可能没有失效的键位,即第一行为空行的情况。这时候不能用 scanf 输入,而要用 cin.getline(str, maxn) 输入字符串。 #include <iostream> #include <cstring> using namespace std; const int maxn = 100001; int main() { int hash_table[256] = {0}; char wrong[257], input[maxn]; cin.getline(wrong, 257); int len = strlen(wrong); for (int i = 0; i < len; i++) { if (wrong[i] >= 'A' && wrong[i] <= 'Z') { wrong[i] = wrong[i] - 'A' + 'a'; } hash_table[wrong[i]] = 1; } scanf("%s", input); len = strlen(input); for (int i = 0; i < len; i++) { if (input[i] >= 'A' && input[i] <= 'Z') { int low = input[i] - 'A' + 'a'; if (hash_table[low] == 0 && hash_table['+'] == 0) { printf("%c", input[i]); } } else if (hash_table[input[i]] == 0) { printf("%c", input[i]); } } printf("\n"); return 0; }

March 26, 2020 · 1 min

PAT甲级 1033 To Fill or Not to Fill (25分)

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. ...

March 22, 2020 · 3 min

PAT甲级 1095 Cars on Campus (30分)

Zhejiang University has 8 campuses and a lot of gates. From each gate we can collect the in/out times and the plate numbers of the cars crossing the gate. Now with all the information available, you are supposed to tell, at any specific time point, the number of cars parking on campus, and at the end of the day find the cars that have parked for the longest time period. ...

March 22, 2020 · 4 min

PAT甲级 1016 Phone Bills (25分)

A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records. ...

March 20, 2020 · 4 min

PAT甲级 1012 The Best Rank (25分)

To evaluate the performance of our first year CS majored students, we consider their grades of three courses only: C - C Programming Language, M - Mathematics (Calculus or Linear Algrbra), and E - English. At the mean time, we encourage students by emphasizing on their best ranks – that is, among the four ranks with respect to the three courses and the average grade, we print the best rank for each student. ...

March 20, 2020 · 3 min

PAT甲级 1082 Read Number in Chinese (25分)

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai. ...

March 19, 2020 · 2 min

PAT甲级 1061 Dating (20分)

Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkggEdsb s&hgsfdk d&Hyscvnm. It took him only a minute to figure out that those strange strings are actually referring to the coded time Thursday 14:04 – since the first common capital English letter (case sensitive) shared by the first two strings is the 4th capital letter D, representing the 4th day in a week; the second common character is the 5th capital letter E, representing the 14th hour (hence the hours from 0 to 23 in a day are represented by the numbers from 0 to 9 and the capital letters from A to N, respectively); and the English letter shared by the last two strings is s at the 4th position, representing the 4th minute. Now given two pairs of strings, you are supposed to help Sherlock decode the dating time. ...

March 18, 2020 · 3 min

PAT乙级 1010 一元多项式求导 (25分)

设计函数求一元多项式的导数。(注:x^n(n为整数)的一阶导数为nx^{n−1}。) 输入格式: 以指数递降方式输入多项式非零项系数和指数(绝对值均为不超过 1000 的整数)。数字间以空格分隔。 输出格式: 以与输入相同的格式输出导数多项式非零项的系数和指数。数字间以空格分隔,但结尾不能有多余空格。注意“零多项式”的指数和系数都是 0,但是表示为 0 0。 输入样例: 3 4 -5 2 6 1 -2 0 输出样例: 12 3 -10 1 6 0 注意点 用 while……EOF 的格式来读入系数和指数(黑框中触发 EOF 要按 ctrl + Z,并回车) 经测试,该题的指数都是非负整数 求导必须从低次项枚举到高次项,否则结果可能为出错 在求导后,当前系数必须清空为0,否则当前无法被后面覆盖 如果求导之后没有任何非零项,需要输出 0 0 #include <cstdio> const int maxn = 1001; int main() { int n[maxn] = {0}, c, e, cnt = 0; while (scanf("%d%d", &c, &e) != EOF) { n[e] = c; } n[0] = 0; for (int i = 1; i < maxn; i++) { n[i - 1] = i * n[i]; n[i] = 0; if (n[i - 1] != 0) { cnt++; } } if (cnt == 0) { printf("0 0"); } else { for (int i = maxn - 1; i >= 0; i--) { if (n[i] != 0) { printf("%d %d", n[i], i); cnt--; if (cnt != 0) { printf(" "); } else { break; } } } } return 0; }

March 17, 2020 · 1 min