PATAdvanceLevel(甲级)1036

题目链接:1036 Boys vs Girls (25 分)

题目描述如下:

作者: CHEN, Yue
单位: 浙江大学
时间限制: 400 ms
内存限制: 64 MB
代码长度限制: 16 KB
1036 Boys vs Girls (25 分)
This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.

Input Specification:
Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student’s name, gender, ID and grade, separated by a space, where name and ID are strings of no more than 10 characters with no space, gender is either F (female) or M (male), and grade is an integer between 0 and 100. It is guaranteed that all the grades are distinct.
Output Specification:
For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF−gradeM. If one such kind of student is missing, output Absent in the corresponding line, and output NA in the third line instead.
Sample Input 1:
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95
Sample Output 1:
Mary EE990830
Joe Math990112
6
Sample Input 2:
1
Jean M AA980920 60
Sample Output 2:
Absent
Jean AA980920
NA

题目大意:

(仅翻译了题目的大致意思并未完全翻译)
题目的大致意思是要求你找出所有男生中的最低成绩与所有女生中的最高成绩之间的差。
输入
第一行输入一个正整数N,然后有N行学生信息。每一行包含学生的姓名、性别、ID和成绩用空格分隔。姓名和ID都是不超过10个字符且不含空格的字符串,性别中M表示男生,F表示女生,成绩在0到100之间。保证所有的成绩都不同。
输出
对每一个测试样例输出3行,第一行输出成绩最高的女生的姓名和ID,第二行输出成绩最低的男生的姓名和ID,第三行输出女生成绩-男生成绩的差值。如果没有这样的学生在相应的行数输出“Absent”并且在最后一行输出“NA”

解题思路:

先初始化女生的成绩为最低成绩0,男生的成绩为最高成绩100,男生女生的名字都“Absent”。然后接收输入并进行判断。如果是男生且成绩比初始化的成绩低更新成绩为此男生成绩,名字为此男生名字,ID为此男生ID。如果是女生且成绩比初始化成绩高更新成绩为此女生成绩,名字为此女生名字,ID为此女生ID。最后进行判断输出即可。此题解题代码使用的是python语言。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
gradeF = 0  #女生初始化成绩
gradeM = 100 #男生初始化成绩
nameF = "Absent" #男生和女生的初始化名字都是Absent
nameM = "Absent"

n = eval(input())

for i in range(n):
s = input()
x = s.split(' ') #用空格分隔出姓名、性别、ID和成绩并通过性别和成绩判断是否需要更新当前值
if x[1] == 'M':
if int(x[3]) <= gradeM:
nameM = x[0]
IDM = x[2]
gradeM = int(x[3])
else:
if int(x[3]) >= gradeF:
nameF = x[0]
IDF = x[2]
gradeF = int(x[3])



if nameF == "Absent": #根据名字判断输出方式并进行输出
print(nameF)
if nameM == "Absent":
print(nameM)
else:
print(nameM + ' ' + IDM)
print("NA")
elif nameM == "Absent":
print(nameF + ' ' + IDF)
print(nameM)
print("NA")
else:
print(nameF + ' ' + IDF)
print(nameM + ' ' + IDM)
print(gradeF - gradeM)

总结

此题难度不大,仅需要注意成绩姓名初始化还有最后输出方式的判断即可。

谢谢阅读,希望对你有所收获。