华为OD机试(yyyy-mm) 题目二

本文介绍了一种计算三个矩形相交面积的方法。通过输入每个矩形左上角的坐标及其长宽,利用Java编程实现计算并输出三个矩形相交部分的面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

求三个矩形的相交面积

输入:
1 6 4 4
2 5 3 3
0 3 5 5
解释:

三行为三个矩形,第一二列为左上角 横纵坐标,第三四列分别为矩形长宽

输出:
3
public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int a1 = input.nextInt();
        int a2 = input.nextInt();
        int a3 = input.nextInt();
        int a4 = input.nextInt();
        int b1 = input.nextInt();
        int b2 = input.nextInt();
        int b3 = input.nextInt();
        int b4 = input.nextInt();
        int c1 = input.nextInt();
        int c2 = input.nextInt();
        int c3 = input.nextInt();
        int c4 = input.nextInt();
        int[] a = {a1, a2, a3, a4};
        int[] b = {b1, b2, b3, b4};
        int[] c = {c1, c2, c3, c4};

        int[] m = commonSpace(a, b);
        int[] commonSpace = commonSpace(m, c);
        System.out.println(commonSpace[2] * commonSpace[3]);

    }

    public static int[] commonSpace(int[] a, int[] b){
        int x11 = a[0];
        int y11 = a[1];
        int x12 = a[0]+a[2];
        int y12 = a[1]-a[3];

        int x21 = b[0];
        int y21 = b[1];
        int x22 = b[0]+b[2];
        int y22 = b[1]-b[3];

        int xx = Math.max(x11,x21);
        int yy = Math.min(y11,y21);

        int x = Math.min(x12,x22);
        int y = Math.max(y12,y22);

        return new int[]{xx,yy,Math.abs(xx-x),Math.abs(yy-y)};
    }

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值