华为OD 矩形相交面积

题目描述

给出3组点坐标(x, y, w, h),-1000<x,y<1000,w,h为正整数。
(x, y, w, h)表示平面直角坐标系中的一个矩形:
x, y为矩形左上角坐标点,w, h向右w,向下h。
(x, y, w, h)表示x轴(x, x+w)和y轴(y, y-h)围成的矩形区域;
(0, 0, 2, 2)表示 x轴(0, 2)和y 轴(0, -2)围成的矩形区域;
(3, 5, 4, 6)表示x轴(3, 7)和y轴(5, -1)围成的矩形区域;
求3组坐标构成的矩形区域重合部分的面积。

输入描述

3行输入分别为3个矩形的位置,分别代表“左上角x坐标”,“左上角y坐标”,“矩形宽”,“矩形高” -1000 <= x,y < 1000

输出描述

输出3个矩形相交的面积,不相交的输出0。

用例

输入1 6 4 4
3 5 3 4
0 3 7 3
输出2
import java.util.Scanner;
import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[][] nums = new int[3][4];
        for(int i = 0; i < 3; i++){
            nums[i] = Arrays.stream(in.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray();
        }
        int w = Math.min(nums[0][0] + nums[0][2], Math.min(nums[1][0] + nums[1][2], nums[2][0] + nums[2][2])) - Math.max(nums[0][0], Math.max(nums[1][0], nums[2][0]));
        int h = Math.min(nums[0][1], Math.min(nums[1][1], nums[2][1])) - Math.max(nums[0][1] - nums[0][3], Math.max(nums[1][1] - nums[1][3], nums[2][1] - nums[2][3]));
        if(w <= 0 || h <= 0){
            System.out.println(0);
        }else{
            System.out.println(w * h);
        }
    }
    
        
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值