14:铺地毯

一、题目链接

http://noi.openjudge.cn/ch0109/14/

二、解题思路

三、实施步骤

四、Java程序

import java.util.Scanner;

class Carpet { // 地毯信息类
    private int x; // 地毯左下角x坐标
    private int y; // 地毯左下角y坐标
    private int width; // 地毯宽度
    private int height; // 地毯高度

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

public class Main {
    /**
     * 返回给定坐标最上层的地毯编号
     *
     * @param carpets Carpet类型的数组,存储地毯集合
     * @param x       int类型的整数,代表给定坐标的x坐标
     * @param y       int类型的整数,代表给定坐标的y坐标
     * @return int类型的整数,代表(x,y)坐标最上层的地毯编号
     */
    public int topCarpet(Carpet[] carpets, int x, int y) {
        int n = carpets.length; // 地毯的数量
        int ans = -1; // 返回结果,初始时为-1
        /* 按照编号从小到大,遍历每一张地毯 */
        for (int i = 0; i < n; i++) {
            // 如果当前地毯能够覆盖给定坐标
            if (x >= carpets[i].getX() && y >= carpets[i].getY()
                 && x <= carpets[i].getX() + carpets[i].getWidth()
                 && y <= carpets[i].getY() + carpets[i].getHeight()) {
                ans = i + 1; // 将ans更新为当前地毯的编号
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        Main test = new Main();
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        Carpet[] carpets = new Carpet[n];
        for (int i = 0; i < n; i++) {
            carpets[i] = new Carpet();
            carpets[i].setX(input.nextInt());
            carpets[i].setY(input.nextInt());
            carpets[i].setWidth(input.nextInt());
            carpets[i].setHeight(input.nextInt());
        }
        int x = input.nextInt();
        int y = input.nextInt();
        System.out.print(test.topCarpet(carpets, x, y));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

江苏科技大学_计算机学院_潘磊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值