The basic exercises of the blue bridge cup test rectangular area intersection BASIC-18 JAVA

Foreword

I have been conducting interviews recently. Many of the written code is too lazy to post to the blog. It is now filled in, but there may be fewer comments. If you have any questions, please contact me

Exam questions basic exercises rectangular area intersection

Resource limit
Time limit: 1.0s Memory limit: 512.0MB

Problem description
  There are two rectangles on the plane, and their sides are parallel to the X axis or Y axis of the rectangular coordinate system. For each rectangle, we give the coordinates of a pair of relative vertices, please program to calculate the area of ​​the intersection of the two rectangles.

Input format The
  input contains only two lines, each line describing a rectangle.
  In each row, the coordinates of a pair of relative vertices of the rectangle are given. The coordinates of each point are represented by two real numbers whose absolute value does not exceed 10 ^ 7.

Output format The
  output contains only one real number, which is the area of ​​the intersection, and is reserved to two decimal places.

Sample input
1 1 3 3
2 2 4 4

Sample output
1.00

Questions for this question

import java.util.Scanner;

public class IntersectRectangle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double x1 = sc.nextDouble(), y1 = sc.nextDouble(), x2 = sc.nextDouble(), y2 = sc.nextDouble();
        double x3 = sc.nextDouble(), y3 = sc.nextDouble(), x4 = sc.nextDouble(), y4 = sc.nextDouble();
        sc.close();
        if (x1 > x2) {
            double tmp = x1;
            x1 = x2;
            x2 = tmp;
        }
        if (y1 > y2) {
            double tmp = y1;
            y1 = y2;
            y2 = tmp;
        }
        if (x3 > x4) {
            double tmp = x3;
            x3 = x4;
            x4 = tmp;
        }
        if (y3 > y4) {
            double tmp = y3;
            y3 = y4;
            y4 = tmp;
        }
        double x = Math.max(x1, x3);
        double X = Math.min(x2, x4);
        double y = Math.max(y1, y3);
        double Y = Math.min(y2, y4);
        if (X - x < 0 || Y - y < 0) {
            System.out.printf("%.2f", 0.00);
        } else {
            System.out.printf("%.2f", (X - x) * (Y - y));
        }
    }
}
Published 113 original articles · Liked 105 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/weixin_43124279/article/details/105419244