CCF-窗口-java

18 篇文章 0 订阅

题目挺长,但仔细读下来,细细分析也是有一点思路的。
N个窗口,每个窗口两个坐标(x1,y1),(x2,y2)序号s
M个待测点,每个点坐标(x,y)
求每个点在哪个窗口内,若在多个窗口内,则取最顶层窗口。

  1. 第一个难题:键盘输入数据如何保存和处理

封装Window类
属性:x1,y1,x2,y2,s;
方法:check(int , int )检测点是否在本窗口;
将所有的window放进一个list中。

对于待测点,不需要另存。键盘输入一个点,直接检测该点,将测的结果存起来即可。

  1. 第二个难题:判断点在哪个窗口中
    窗口:(x1,y1),(x2,y2)
    点坐标(x,y)
    点在窗口内满足:x1<=x<=x2 && y1<=y<=y2

  2. 第三个难题:将被选择窗口置于顶层

这个难题是我最后看了很久才解决的。

输入一个点,遍历window-list,如果某个窗口被选择了,就将该窗口的序号索引标记。如果也在下一个窗口,则下一个窗口序号覆盖上一个序号(因为顶层窗口优先)

序号用于返回点在哪个窗口。
索引用于当前被选择窗口和顶层窗口互换位置。

刚开始我只是返回了序号,然后将window-list(num-1)与window-list(n-1)互换位置,最终提交代码测试样例通过但是只有90分。后来在我的仔细观察下发现当前被选择窗口并不是的索引并不是num-1。

  1. 第四个难题:如何将两个窗口互换位置

我们在排序中经常将两个数互换位置:
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
可以发现其实我们互换的是两个位置里的内容。
所以对于两个对象来说,互换位置同样是互换连个位置里的属性

千辛万苦的100分代码终于来了!

我的测试样例:
在这里插入图片描述
在这里插入图片描述


```java
import java.util.*;
public class _201403_2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int N = scanner.nextInt();
        int M = scanner.nextInt();
        if (N<1 || N>10){
            return;
        }
        if (M<1 || M>10){
            return;
        }
        List<Window> windowslist = new ArrayList<>();
        for (int i=1;i<=N;i++){
            windowslist.add(new Window(scanner.nextInt(),scanner.nextInt(),
                    scanner.nextInt(),scanner.nextInt(),i));
        }
        List<String> resultlist = new ArrayList<>();
        for (int i=1;i<=M;i++){
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            int num=0;int index=0;
            for (int j=0;j<N;j++){
                int check = windowslist.get(j).check(x,y);
                if (check!=0){
                    num = check;
                    index = j;
                }
            }
            if (num==0){
                resultlist.add("IGNORED");
            }else{
                resultlist.add(num+"");
                Window current = windowslist.get(index);
                Window top = windowslist.get(N-1);
                Window tmp = new Window(current.getX1(),current.getY1(),current.getX2(),current.getY2(),current.getS());
                current.setX1(top.getX1());current.setY1(top.getY1());current.setX2(top.getX2());current.setY2(top.getY2());current.setS(top.getS());
                top.setX1(tmp.getX1());top.setY1(tmp.getY1());top.setX2(tmp.getX2());top.setY2(tmp.getY2());top.setS(tmp.getS());
            }
        }
        for (String s:resultlist){
            System.out.println(s);
        }
    }
}
class Window{
    int x1,y1, x2,y2;
    int s;
    public Window(int x1,int y1,int x2,int y2,int s){
        this.x1=x1;this.y1=y1;
        this.x2=x2;this.y2=y2;
        this.s=s;
    }
    public int check(int x,int y){
        if (x>=x1 && x<=x2 && y>=y1 && y<=y2){
            return s;
        }else{
            return 0;
        }
    }

    public int getX1() {
        return x1;
    }

    public void setX1(int x1) {
        this.x1 = x1;
    }

    public int getY1() {
        return y1;
    }

    public void setY1(int y1) {
        this.y1 = y1;
    }

    public int getX2() {
        return x2;
    }

    public void setX2(int x2) {
        this.x2 = x2;
    }

    public int getY2() {
        return y2;
    }

    public void setY2(int y2) {
        this.y2 = y2;
    }

    public int getS() {
        return s;
    }

    public void setS(int s) {
        this.s = s;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值