python数据结构与算法练习-排序
仅记录刷题过程以及需要注意的知识点,方便自己复习。
Nth Largest Value
链接: link.
For this problem, you will write a program that prints the Nth largest value in a fixed sized array of integers. To make things simple, N will be 3 and the array will always be have 10 decimal integer values.
输入格式
The first line of input contains a single integer §, ((1 \leq P \leq 1000)), which is the number of data sets that follow. Each data set consists of a single line containing the data set number, followed by a space, followed by 10 space separated decimal integers whose values are between 1 and 1000 inclusive.
输出格式
For each data set, generate one line of output with the following values: The data set number as a decimal integer, a space, and the 3rd largest value of the corresponding 10 integers.
input
4
1 1 2 3 4 5 6 7 8 9 1000
2 338 304 619 95 343 496 489 116 98 127
3 931 240 986 894 826 640 965 833 136 138
4 940 955 364 188 133 254 501 122 768 408
output
1 8
2 489
3 931
4 768
python实现
#实现:
n = int(input())
for i in range(1,n+1):
a = list(map(int,input().split()))[1:]
a.sort()
print(str(i)+" "+str(a[-3]))
#当需要将多行输入保存为变量时,可用如下形式:
n = int(input())
a = []
for i in range(1,n+1):
ai = list(map(int,input().split()))[1:]
ai.sort()
a.append((i,ai[-3]))
for i in a:
print(" ".join(str(x) for x in i))