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]);
		}
	}
}
原文地址:https://www.cnblogs.com/yu-jiawei/p/12342912.html