• 201403-2 窗口 Java



    要想到定义一个窗口类,判断点在不在矩形里好判断
    需要一个数组,存放结果

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    
    class Window{
    	int x1;
    	int y1;
    	int x2;
    	int y2;
    	int windowNum;//窗口顺序
    	public Window(int x1, int y1, int x2, int y2, int windowNum) {
    		super();
    		this.x1 = x1;
    		this.y1 = y1;
    		this.x2 = x2;
    		this.y2 = y2;
    		this.windowNum = windowNum;
    	}
    	//判断点在哪个窗口
    	public int inWindow(int x,int y) {
    		if(x >= this.x1 && x <= this.x2 && y >= this.y1 && y <=this.y2) {
    			return this.windowNum;
    		}else {
    			return 0;
    		}
    	}
    }
    public class Main {
    	public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
    		int N = sc.nextInt();//N个窗口,先输入的在最底层
    		int M = sc.nextInt();//点击了几次
    		
    		List<Window> windows = new ArrayList<Window>();
    		
    		for(int i = 0;i<N;i++) {
    			windows.add(new Window(sc.nextInt(),sc.nextInt(),sc.nextInt(),sc.nextInt(),i+1));
    		}
    		//存放结果
    		String [] result = new String[M];
    		
    		int num = 0;
    		
    		for(int i = 0;i<M;i++) {
    			int x = sc.nextInt();
    			int y = sc.nextInt();
    			int j = windows.size() - 1;//最后一个打开的窗口在最前面
    			for(;j>=0;j--) {
    				Window currentWindow = windows.get(j);//获取当前窗口
    				int windowNum = currentWindow.inWindow(x, y);
    				if(windowNum > 0) {
    					result[num] = windowNum + "";//将被点击的窗口放到最前面
    					windows.add(currentWindow);
    					windows.remove(j);
    					break;
    				}
    			}
    			if(j < 0) {
    				result[num] = "IGNORED";
    			}
    			num++;
    		}
    		sc.close();
    		for(int i = 0;i<M;i++) {
    			System.out.println(result[i]);
    		}
    	}
    }
    
  • 相关阅读:
    Hadoop分布式文件系统:架构和设计
    分布式设计学习资料
    codeforces上一道贪心算法题
    优先队列实现n路归并算法O(n * lgK)
    LINUX 暂停、继续进程
    重叠(Overlapped)IO模型
    WSAEventSelect模型
    WSAEventSelect模型 应用实例,重写TCP服务器实例
    选择模型2
    第四章 数据抽象 《C++编程思想》
  • 原文地址:https://www.cnblogs.com/yu-jiawei/p/12342912.html
Copyright © 2020-2023  润新知