AOJ0001 List of Top 3 Hills【水题】
程序员文章站
2024-02-19 12:40:34
...
List of Top 3 Hills
Aizu - 0001There 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
问题链接:AOJ0001List of Top 3 Hills
问题简述:(略)
问题分析:
输入若干整数表示山的高度,求其中最高的三座山。
这个问题也就是求最大的三个数问题。
把数据放入数组进行排序,然后找出最大的三个数,是一种做法,那是浮云。
本程序逻辑更加简洁一些,算法复杂度也低,还不需要数组存储数据,一边输入一边处理。
最大的三个数放在变量max中,并且总是满足max1>=max2>=max3。这样找最大三个数就变得比较简单了。
题记:(略)
参考链接:(略)
AC的C语言程序如下:
/* AOJ0001 List of Top 3 Hills */
#include <stdio.h>
#include <limits.h>
int main(void)
{
int a, max1, max2, max3, temp;
max1 = max2 = max3 = INT_MIN;
while(~scanf("%d", &a)) {
if(a > max3) {
max3 = a;
/* 排序 */
if(max3 > max1) {
temp = max3;
max3 = max2;
max2= max1;
max1 = temp;
} else if(max3 > max2) {
temp = max3;
max3 = max2;
max2 = temp;
}
}
}
printf("%d\n%d\n%d\n", max1, max2, max3);
return 0;
}