【蓝桥杯】[基础练习VIP]矩形面积交(Java实现)

【蓝桥杯】[基础练习VIP]矩形面积交(Java实现)

Java代码实现:

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		double x1,x2,x3,x4;
		double y1,y2,y3,y4;
		x1 = scanner.nextDouble();
		y1 = scanner.nextDouble();
		x2 = scanner.nextDouble();
		y2 = scanner.nextDouble();
		
		x3 = scanner.nextDouble();
		y3 = scanner.nextDouble();
		x4 = scanner.nextDouble();
		y4 = scanner.nextDouble();
		
		double m1,n1;//交集相对左顶点坐标
		double m2,n2;//交集相对右顶点坐标
		
		m1 = Math.max(Math.min(x1, x2), Math.min(x3, x4));
		n1 = Math.max(Math.min(y1, y2), Math.min(y3, y4));
		
		m2 = Math.min(Math.max(x1, x2), Math.max(x3, x4));
		n2 = Math.min(Math.max(y1, y2), Math.max(y3, y4));
		
		double width = m2 - m1;
		double length = n2 - n1;
		double s = width * length;
		
		if (m2 > m1 && n2 > n1) {
			System.out.printf("%.2f", s);
		} else {
			System.out.println("0.00");
		}
		
	}

}