List of Top 3 Hills
Problem
There is a data which provides heights (in meter) of mountains. The data is only for ten mountains.
Write a program which prints heights of the top three mountains in descending order.
Input
Height of mountain 1
Height of mountain 2
Height of mountain 3
.
.
Height of mountain 10
Constraints
0 ≤ height of mountain (integer) ≤ 10,000
Output
Height of the 1st mountain
Height of the 2nd mountain
Height of the 3rd mountain
Sample Input 1
1819
2003
876
2840
1723
1673
3776
2848
1592
922
Output for the Sample Input 1
3776
2848
2840
Sample Input 2
100
200
300
400
500
600
700
800
900
900
Output for the Sample Input 2
900
900
800
题目大意
输入十座山的高度,依次输出前三高的高度。
题目解读
没仔细读题,当成了数量不定的若干座山来做。
算法
循环读入到文件结束,对于每一个数值,判断其是否高于当前存储的最大值,是则交换,再依次与次大、第三大值比较、交换,算法时间复杂度 O(N)。
代码
1 h1 = h2 = h3 = 0 2 while (1): 3 try: 4 h = int(input()) 5 except EOFError: 6 break 7 if (h1 < h): 8 h1, h = h, h1 9 if (h2 < h): 10 h2, h = h, h2 11 if (h3 < h): 12 h3, h = h, h3 13 print(h1) 14 print(h2) 15 print(h3)
代码解读
注意:以下内容完全根据笔者自己对 Python 3 的理解胡说八道。
a, b = b, a:Python 3 的特殊的进行变量交换方式。