ccf 202303-1田地丈量

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
//        Rect rect1 = new Rect(0,0,10,10);
//        Rect rect2 = new Rect(0,0,5,5);
//        System.out.println(rect1.getRectArea(rect2));
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        int right_up_x = scanner.nextInt();
        int right_up_y = scanner.nextInt();

        Rect rect = new Rect(0,0,right_up_x,right_up_y);

        List<Rect> rectList = new ArrayList<>();
        for(int i = 0; i < num; i++){
            int x1 = scanner.nextInt();
            int y1 = scanner.nextInt();
            int x2 = scanner.nextInt();
            int y2 = scanner.nextInt();
            Rect tmp = new Rect(x1,y1,x2,y2);
            rectList.add(tmp);
        }
        int total = 0;
        for(Rect recttmp : rectList){
            int tmp = recttmp.getRectArea(rect);
            total += tmp;
        }
        System.out.println(total);
    }

}

class Rect {
    int left_down_x;
    int left_down_y;

    int right_up_x;
    int right_up_y;

    public Rect(int left_down_x, int left_down_y, int right_up_x, int right_up_y) {
        this.left_down_x = left_down_x;
        this.left_down_y = left_down_y;
        this.right_up_x = right_up_x;
        this.right_up_y = right_up_y;
    }

    public int getRectArea(Rect other){
        if(this.left_down_x >= other.right_up_x
                || other.left_down_x >= this.right_up_x
                || this.left_down_y >= other.right_up_y
                || other.left_down_y >= this.right_up_y){
            return 0;
        }

        int sub_rect_left_down_x = Math.max(this.left_down_x, other.left_down_x);
        int sub_rect_right_up_x = Math.min(this.right_up_x, other.right_up_x);
        int sub_rect_left_down_y = Math.max(this.left_down_y, other.left_down_y);
        int sub_rect_right_up_y = Math.min(this.right_up_y, other.right_up_y);

        return (sub_rect_right_up_x-sub_rect_left_down_x)*(sub_rect_right_up_y-sub_rect_left_down_y);

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值