L1-026. I Love GPLT-PAT团体程序设计天梯赛GPLT

这道超级简单的题目没有任何输入。
你只需要把这句很重要的话 —— “I Love GPLT”——竖着输出就可以了。
所谓“竖着输出”,是指每个字符占一行(包括空格),即每行只能有1个字符和回车。 

L1-025. 正整数A+B-PAT团体程序设计天梯赛GPLT

本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000]。稍微有点麻烦的是,输入并不保证是两个正整数。
输入格式:
输入在一行给出A和B,其间以空格分开。问题是A和B不一定是满足要求的正整数,有时候可能是超出范围的数字、负数、带小数点的实数、甚至是一堆乱码。
注意:我们把输入中出现的第1个空格认为是A和B的分隔。题目保证至少存在一个空格,并且B不是一个空字符串。
输出格式:
如果输入的确是两个正整数,则按格式“A + B = 和”输出。如果某个输入不合要求,则在相应位置输出“?”,显然此时和也是“?”。
输入样例1:
123 456
输出样例1:
123 + 456 = 579
输入样例2:
22. 18
输出样例2:
? + 18 = ?
输入样例3:
-100 blabla bla…33
输出样例3:
? + ? = ?
分析:用string t接收一行的字符串,然后以第一个空格分离成字符串a和b。
islegal判定字符串a、b是否合法,不合法的赋值”?”

 

1008. Elevator (20)-PAT甲级真题

The highest building in our city has only one elevator. A request list is made up with N positive numbers. The numbers denote at which floors the elevator will stop, in specified order. It costs 6 seconds to move the elevator up one floor, and 4 seconds to move down one floor. The elevator will stay for 5 seconds at each stop.

For a given request list, you are to compute the total time spent to fulfill the requests on the list. The elevator is on the 0th floor at the beginning and does not have to return to the ground floor when the requests are fulfilled.

Input Specification:

Each input file contains one test case. Each case contains a positive integer N, followed by N positive numbers. All the numbers in the input are less than 100.

Output Specification:

For each test case, print the total time on a single line.

Sample Input:

3 2 3 1

Sample Output:

41

题目大意:电梯从0层开始向上,给出该电梯依次按顺序停的楼层数,并且已知上升需要6秒/层,下降需要4秒/层,停下来的话需要停5秒,问走完所有需要停的楼层后总共花了多少时间~

分析:累加计算输出~now表示现在的层数,a表示将要去的层数,当a > now,电梯上升,需要6 * (a – now)秒,当a < now,电梯下降,需要4 * (now – a)秒,每一次需要停5秒,最后输出累加的结果sum~

 

1109. Group Photo (25)-PAT甲级真题

Formation is very important when taking a group photo. Given the rules of forming K rows with N people as the following:

The number of people in each row must be N/K (round down to the nearest integer), with all the extra people (if any) standing in the last row;
All the people in the rear row must be no shorter than anyone standing in the front rows;
In each row, the tallest one stands at the central position (which is defined to be the position (m/2+1), where m is the total number of people in that row, and the division result must be rounded down to the nearest integer);
In each row, other people must enter the row in non-increasing order of their heights, alternately taking their positions first to the right and then to the left of the tallest one (For example, given five people with their heights 190, 188, 186, 175, and 170, the final formation would be 175, 188, 190, 186, and 170. Here we assume that you are facing the group so your left-hand side is the right-hand side of the one at the central position.);
When there are many people having the same height, they must be ordered in alphabetical (increasing) order of their names, and it is guaranteed that there is no duplication of names.
Now given the information of a group of people, you are supposed to write a program to output their formation.

Input Specification:

Each input file contains one test case. For each test case, the first line contains two positive integers N (<=10000), the total number of people, and K (<=10), the total number of rows. Then N lines follow, each gives the name of a person (no more than 8 English letters without space) and his/her height (an integer in [30, 300]).

Output Specification:

For each case, print the formation — that is, print the names of people in K lines. The names must be separated by exactly one space, but there must be no extra space at the end of each line. Note: since you are facing the group, people in the rear rows must be printed above the people in the front rows.

Sample Input:
10 3
Tom 188
Mike 170
Eva 168
Tim 160
Joe 190
Ann 168
Bob 175
Nick 186
Amy 160
John 159
Sample Output:
Bob Tom Joe Nick
Ann Mike Eva
Tim Amy John

题目大意:拍集体照时队形很重要,这里对给定的N个人K排的队形设计排队规则如下:
每排人数为N/K(向下取整),多出来的人全部站在最后一排;后排所有人的个子都不比前排任何人矮;每排中最高者站中间(中间位置为m/2+1,其中m为该排人数,除法向下取整);每排其他人以中间人为轴,按身高非增序,先右后左交替入队站在中间人的两侧(例如5人身高为190、188、186、175、170,则队形为175、188、190、186、170。这里假设你面对拍照者,所以你的左边是中间人的右边);若多人身高相同,则按名字的字典序升序排列。这里保证无重名。现给定一组拍照人,请编写程序输出他们的队形。输出拍照的队形。即K排人名,其间以空格分隔,行末不得有多余空格。注意:假设你面对拍照者,后排的人输出在上方,前排输出在下方~

分析:建立结构体node,里面包含string类型的姓名name和int类型的身高height~将学生的信息输入到node类型的vector数组stu中~然后对stu数组进行排序(cmp函数表示排序规则,如果身高不等,就按照身高从大到小排列;如果身高相等,就按照名字从小到大的字典序排列~)然后用while循环排列每一行,将每一行应该排列的结果的姓名保存在ans数组中~

因为是面对拍照者,后排的人输出在上方,前排输出在下方,每排人数为N/K(向下取整),多出来的人全部站在最后一排,所以第一排输出的应该是包含多出来的人,所以while循环体中,当row == k时,表示当前是在排列第一行,那么这一行的人数m应该等于总人数n减去后面的k列*(k-1)行,即m = n – n / k * (k-1);如果不是第一行,那么m直接等于n / k;最中间一个学生应该排在m/2的下标位置,即ans[m / 2] = stu[t].name;然后排左边一列,ans数组的下标 j 从m/2-1开始,一直往左j–,而对于stu的下标 i,是从t+1开始,每次隔一个人选取(即i = i+2,因为另一些人的名字是给右边的),每次把stu[i]的name赋值给ans[j–];排右边的队伍同理,ans数组的下标 j 从m/2 + 1开始,一直往右j++,stu的下标 i,从t+2开始,每次隔一个人选取(i = i+2),每次把stu[i]的name赋值给ans[j++],然后输出当前已经排好的ans数组~每一次排完一列row-1,直到row等于0时退出循环表示已经排列并输出所有的行~

 

1108. Finding Average (20)-PAT甲级真题

The basic task is simple: given N real numbers, you are supposed to calculate their average. But what makes it complicated is that some of the input numbers might not be legal. A “legal” input is a real number in [-1000, 1000] and is accurate up to no more than 2 decimal places. When you calculate the average, those illegal numbers must not be counted in.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<=100). Then N numbers are given in the next line, separated by one space.

Output Specification:

For each illegal input number, print in a line “ERROR: X is not a legal number” where X is the input. Then finally print in a line the result: “The average of K numbers is Y” where K is the number of legal inputs and Y is their average, accurate to 2 decimal places. In case the average cannot be calculated, output “Undefined” instead of Y. In case K is only 1, output “The average of 1 number is Y” instead.

Sample Input 1:
7
5 -3.2 aaa 9999 2.3.4 7.123 2.35
Sample Output 1:
ERROR: aaa is not a legal number
ERROR: 9999 is not a legal number
ERROR: 2.3.4 is not a legal number
ERROR: 7.123 is not a legal number
The average of 3 numbers is 1.38
Sample Input 2:
2
aaa -9999
Sample Output 2:
ERROR: aaa is not a legal number
ERROR: -9999 is not a legal number
The average of 0 numbers is Undefined

分析:用非常好用的sscanf和sprintf即可解决~
sscanf() – 从一个字符串中读进与指定格式相符的数据
sprintf() – 字符串格式化命令,主要功能是把格式化的数据写入某个字符串中

1104. Sum of Number Segments (20)-PAT甲级真题

Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For example, given the sequence {0.1, 0.2, 0.3, 0.4}, we have 10 segments: (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4).

Now given a sequence, you are supposed to find the sum of all the numbers in all the segments. For the previous example, the sum of all the 10 segments is 0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N, the size of the sequence which is no more than 105. The next line contains N positive numbers in the sequence, each no more than 1.0, separated by a space.

Output Specification:

For each test case, print in one line the sum of all the numbers in all the segments, accurate up to 2 decimal places.

Sample Input:
4
0.1 0.2 0.3 0.4
Sample Output:
5.00

题目大意:给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段。例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0.4) 这10个片段。给定正整数数列,求出全部片段包含的所有的数之和。如本例中10个片段总和是0.1 + 0.3 + 0.6 + 1.0 + 0.2 + 0.5 + 0.9 + 0.3 + 0.7 + 0.4 = 5.0,在一行中输出该序列所有片段包含的数之和,精确到小数点后2位~

分析:将数列中的每个数字读取到temp中,假设我们选取的片段中包括temp,且这个片段的首尾指针分别为p和q,那么对于p,有i种选择,即12…i,对于q,有n-i+1种选择,即i, i+1, … n,所以p和q组合形成的首尾片段有i * (n-i+1)种,因为每个里面都会出现temp,所以temp引起的总和为temp * i * (n – i + 1);遍历完所有数字,将每个temp引起的总和都累加到sum中,最后输出sum的值~

【Update 2020-6-18】PS:题目更新了测试样例,导致之前的代码测试点2(第3个测试点)无法通过,研究到凌晨四点找错误原因…后来找到了给PAT这道题提反馈改测试用例的这位同学写的关于这道题的博客:https://blog.zhengrh.com/post/about-double/,感谢他让我找到了错误的原因,大概意思是:N比较大时,double类型的值多次累加导致的精度误差,因为输入为十进制小数,存储到double中时,计算机内部使用二进制表示,且计算机的字长有限,有的十进制浮点数使用二进制无法精确表示只能无限接近,在字长的限制下不可避免会产生舍入误差,这些细微的误差在N较大时多次累加会产生较大误差,所以建议不要使用double类型进行多次累加的精确计算,而是转为能够精确存储的整型。尝试把输入的double类型的值扩大1000倍后转为long long整型累加,同时使用long long类型保存sum的值,输出时除以1000.0转为浮点型再输出(相当于把小数点向后移动3位后再计算,避免double类型的小数部分存储不精确,多次累加后对结果产生影响)
但我觉得乘以1000也未必严谨,可能测试样例最小只有小数点后三位,如果测试样例变成小数点后四位、五位、六位,乘以1000相当于直接在小数点后三位处截断,而原本第四五六位经过多次累加进位后依然可能会引起精度问题,但如果乘以10000就会超出long long的值,我认为最精确的应该是截取到所有小数中最大的位数的那一位。。可能我的想法有疏漏,经过测试,测试样例确实没有超过小数点后三位,虽然修改为乘以1000后代码已经AC,但如果对测试样例稍加修改,可能又会导致不AC了…所以这道题先打个问号吧,我猜可能将来题目样例还会被修改…