首页 > 编程语言 > 详细

201403-2 窗口 Java

时间:2020-02-21 20:43:02      阅读:63      评论:0      收藏:0      [点我收藏+]

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

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]);
        }
    }
}

201403-2 窗口 Java

原文:https://www.cnblogs.com/yu-jiawei/p/12342912.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!