题目1483:求最大最小数
2014-02-10 23:36
330 查看
优先级队列的使用
import java.io.IOException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Scanner;
import java.util.PriorityQueue;
import java.util.Comparator;
class Main
{
public static final boolean DEBUG = false;
public static void main(String[] args) throws IOException
{
Scanner cin;
int n;
if (DEBUG) {
cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
} else {
cin = new Scanner(new InputStreamReader(System.in));
}
while (cin.hasNext()) {
n = cin.nextInt();
if (n == 0) break;
Comparator<Integer> comp = new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b - a;
}
};
PriorityQueue<Integer> qMin = new PriorityQueue<Integer>();
PriorityQueue<Integer> qMax = new PriorityQueue<Integer>(1, comp);
for (int i = 0; i < n; i++) {
int num = cin.nextInt();
qMin.add(num);
qMax.add(num);
}
System.out.println(qMax.peek() + " " + qMin.peek());
}
}
}
import java.io.IOException;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Scanner;
import java.util.PriorityQueue;
import java.util.Comparator;
class Main
{
public static final boolean DEBUG = false;
public static void main(String[] args) throws IOException
{
Scanner cin;
int n;
if (DEBUG) {
cin = new Scanner(new FileReader("d:\\OJ\\uva_in.txt"));
} else {
cin = new Scanner(new InputStreamReader(System.in));
}
while (cin.hasNext()) {
n = cin.nextInt();
if (n == 0) break;
Comparator<Integer> comp = new Comparator<Integer>() {
public int compare(Integer a, Integer b) {
return b - a;
}
};
PriorityQueue<Integer> qMin = new PriorityQueue<Integer>();
PriorityQueue<Integer> qMax = new PriorityQueue<Integer>(1, comp);
for (int i = 0; i < n; i++) {
int num = cin.nextInt();
qMin.add(num);
qMax.add(num);
}
System.out.println(qMax.peek() + " " + qMin.peek());
}
}
}
相关文章推荐
- 题目1483:求最大最小数
- 题目1483:求最大最小数
- 题目1483:求最大最小数
- 题目1483:求最大最小数
- 九度 题目1483:求最大最小数
- 题目1483:求最大最小数
- 九度OJ 题目1483:求最大最小数
- 九度OJ 题目1483:求最大最小数
- 题目1053:互换最大最小数
- 九度考研真题 浙大 2007-2浙大 题目1020:最小长方形 取最大最小值
- 九度 Online Judge 算法 刷题 题目1053:互换最大最小数
- 设计一个从5个整数中取最小数和最大数的程序---acm题目
- 九度OJ—题目1053:互换最大最小数
- POJ2516 Minimum Cost 最大流最小费用典型题目
- 【程序6】 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。
- 【程序35】 题目:输入数组,最大的与第一个元素交换, 最小的与最后一个元素交换,输出数组。
- 数据统计 题目是输入一些整数,求出它们的最大值,最小值和平均值
- 题目1053:互换最大最小数
- POJ2516 Minimum Cost 最大流最小费用典型题目
- 腾讯2017年实习生编程题目(第三题)——有趣的数字 小Q今天在上厕所时想到了这个问题:有n个数,两两组成二元组,差最小的有多少对呢?差最大呢?