# Demo entry 6355110

ss

Submitted by anonymous on Apr 12, 2017 at 13:27
Language: Java. Code size: 2.0 kB.

```package day06;

import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Scanner;

public class Solution {

public static void main(String[] args) {
Solution ss = new Solution();
System.out.print("plz input the amount of number:");
Scanner scan = new Scanner(System.in);
int count = scan.nextInt();
ss.solution(count);
}
public void solution(int count){
Random random = new Random(new java.util.Date().getTime());
int i = 0;
PriorityQueue<Integer> maxheap = new PriorityQueue<Integer>(10, new Comparator<Integer>() {

@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}

});
PriorityQueue<Integer> minheap = new PriorityQueue<Integer>();
while(i < count){
int num = random.nextInt() % 1000;
if(minheap.size() == 0 && maxheap.size() == 0){
i++;
continue;
}
if(num > maxheap.peek()){
if(minheap.size() - maxheap.size() >= 2){
int temp = minheap.poll();
}
}else{
if(maxheap.size() - minheap.size() >= 2){
int temp = maxheap.poll();
}
}
System.out.println("less than median (maxheap)");
for(Iterator<Integer> iter = maxheap.iterator(); iter.hasNext();){
System.out.print(iter.next() + " ");
}
System.out.println();
System.out.println("greater than median (minheap)");
for(Iterator<Integer> iter = minheap.iterator(); iter.hasNext();){
System.out.print(iter.next() + " ");
}
System.out.println();
System.out.print("median is: ");
if(maxheap.size() > minheap.size()){
System.out.println(maxheap.peek());
}else if(maxheap.size() < minheap.size()){
System.out.println(minheap.peek());
}else{
System.out.println(((float)minheap.peek()+maxheap.peek())/2);
}
i++;
}
}
}
```

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.