您的位置:首页 > 其它

题目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());
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: