码迷,mamicode.com
首页 > 其他好文 > 详细

List of Top 3 Hills

时间:2018-03-25 23:00:10      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:gpo   ons   个数   print   exce   循环   size   str   交换   

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
  
 
 
题目大意
  输入十座山的高度,依次输出前三高的高度。
 
题目解读
  没仔细读题,当成了数量不定的若干座山来做。
 
算法
  循环读入到文件结束,对于每一个数值,判断其是否高于当前存储的最大值,是则交换,再依次与次大、第三大值比较、交换。
 
代码
 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 的特殊的进行变量交换方式。

List of Top 3 Hills

标签:gpo   ons   个数   print   exce   循环   size   str   交换   

原文地址:https://www.cnblogs.com/Efve/p/8647426.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!