我好想再水一百年,hdu1509

  

Windows Message Queue

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4562    Accepted Submission(s): 1821


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!


代码实现:(解题思路,代码解释)

/*
 *     解题思路:首先申明题目很水啊。感觉自己没用优先队列哦,只用了队列而已。我的思想是这样的,根据题目要求是最先考虑信息的优先级,然后如果有相同优先级的信息的话,就把最先
 *进来的 消息输出去。在我看来,利用一个ArrayList做队列,先把输入需要输入的消息,参数和优先级用对象message封装了放到list中,然后在弹出消息的时候只要从最开始找到那个优先级
 *最小的消息找到,并记录好,然而这个消息就是满足条件的消息了,把他从list中弹出。
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		List<message> list=new ArrayList<message>();//利用ArrayList做队列
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			String str = sc.nextLine();    //接收命令
			String[] strs=str.split(" ");  //根据给的案例,可以用空格分隔命令(str[0],str[1],str[2],str[3]分别对应处理命令,消息,参数,优先级)
			//如果是PUT则把对应数据封装好加入list中
			if(strs[0].equals("PUT")){
				list.add(new message(strs[1],strs[2],strs[3]));
			}else{
				//如果list为空,则输出EMPTY QUEUE!
				if(list.size()==0){
					System.out.println("EMPTY QUEUE!");
				}else{
					int outIndex=-1; //用来存放需要输出的消息在list中的位置
					int Priority=Integer.MAX_VALUE; //用来存储优先级,初始化最大
					//遍历list,知道到最早的那个并且优先级最高的消息的在list中的位置
					for(int i=0;i<list.size();i++){
						if(Priority>Integer.parseInt(list.get(i).getPriority())){
							Priority=Integer.parseInt(list.get(i).getPriority());
							outIndex=i;
						}
					}
					//输出
					System.out.println(list.remove(outIndex).toString());
				}
			}
		}
	}
}
//用来封装消息信息的类message
class message{
	private String msg=null;  //消息
	private String num=null;  //参数
	private String Priority=null;  //优先级
	//构造函数
	public message(String msg, String num, String priority) {
		this.msg = msg;
		this.num = num;
		Priority = priority;
	}
	//获取优先级
	public String getPriority() {
		return Priority;
	}
	//toString方法
	@Override
	public String toString() {
		return  msg + " " + num;
	}
}

结果截图:

                                                  我好想再水一百年,hdu1509_第1张图片


你可能感兴趣的:(队列,hdu1509)