#第一周1004解题报告#

来源:互联网 发布:java培训贷款骗局流程 编辑:程序博客网 时间:2024/05/21 07:56

第一周1004解题报告

Problem Description
Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text change, the system will add a message to the queue. Meanwhile, the process will do a loop for getting message from the queue according to the priority value if it is not empty. Note that the less priority value means the higher priority. In this problem, you are asked to simulate the message queue for putting messages to and getting message from the message queue.
Input
There’s only one test case in the input. Each line is a command, “GET” or “PUT”, which means getting message or putting message. If the command is “PUT”, there’re one string means the message name and two integer means the parameter and priority followed by. There will be at most 60000 command. Note that one message can appear twice or more and if two messages have the same priority, the one comes first will be processed first.(i.e., FIFO for the same priority.) Process to the end-of-file.
Output
For each “GET” command, output the command getting from the message queue with the name and parameter in one line. If there’s no message in the queue, output “EMPTY QUEUE!”. There’s no output for “PUT” command.
Sample Input

GET
PUT msg1 10 5
PUT msg2 10 4
GET
GET
GET

Sample Output

EMPTY QUEUE!
msg2 10
msg1 10
EMPTY QUEUE!
题意是信息有入有出,get得信息,put放信息,输入优先级,有优先级相同,按输入先后决定优先级,因此声明一个全局变量i;记录每个数的先后,使用优先队列priorityQueue存数,编写comparator,优先级比较,次之顺序比较,做完后,优先队列会自动排序,遇到get,调用

priorityQueue.poll().toString()

遇到put调用

priorityQueue.add(t1);
java代码:package 第一周;import java.util.Comparator;import java.util.PriorityQueue;import java.util.Queue;import java.util.Scanner;public class Main1004 {    public static class tree {        String s;//PUTorGET        int parameter;        int value;//优先级        int key;//输入顺序        public tree(String s, int parameter, int value, int key) {//初始化构造器            this.s = s;            this.parameter = parameter;            this.value = value;            this.key = key;        }        public String getS1() {            return s;        }        public int getParameter() {            return parameter;        }        public int getValue() {            return value;        }        public int getKey() {            return key;        }        public String toString(){            return s+" "+parameter;        }        }    public static void main(String args[]) {        Comparator<tree> Par = new Comparator<tree>() {//编写Comparator<tree>            public int compare(tree o1, tree o2) {                if (o2.value < o1.value) {//优先级优的选中往前排                    return 1;                } else if (o1.value < o2.value) {                    return -1;                } else {                    if (o2.key < o1.key) {//顺序前的往前排                        return 1;                    } else                        return -1;                }            }        };        Queue<tree> priorityQueue = new PriorityQueue<tree>(11, Par);//优先队列        Scanner sc = new Scanner(System.in);        int i = 0;        while(sc.hasNext()){            String s = sc.nextLine();        if(s.equals("GET"))        {            if(priorityQueue.isEmpty())                System.out.println("EMPTY QUEUE!");            else            {                System.out.println(priorityQueue.poll().toString());            }        }        String s1[]  = s.split("\\s+",4);//读入分割一行输入数据        if(s1[0].equals("PUT")){            int pre =Integer.parseInt(s1[3]);// 优先级            int f =Integer.parseInt(s1[2]);//            tree t1 = new tree(s1[1],f,pre,i);//构造一次            priorityQueue.add(t1);//进队列并自动排序        }        i++;//顺序自增        }    }}
0 0
原创粉丝点击