本文主要是介绍矩形总面积(第十四届蓝桥杯JavaB组省赛真题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
测试用例范围比较大,所以全部用long类型,如果用int类型只能通过60%,建议在内存和运行时间允许的情况下,比赛题都用long。
重点在于计算相交的面积,这里找的两个相交点是左上角(m1,n1)和右下角(m2,n2)
import java.util.*;
// 1:无需package
// 2: 类名必须Main, 不可修改public class Main {public static void main(String[] args) {Scanner scan = new Scanner(System.in);long x1 = scan.nextInt();long y1 = scan.nextInt();long x2 = scan.nextInt();long y2 = scan.nextInt();long x3 = scan.nextInt();long y3 = scan.nextInt();long x4 = scan.nextInt();long y4 = scan.nextInt();long m1 = Math.max(Math.min(x1, x2), Math.min(x3, x4));long n1 = Math.min(Math.max(y1, y2), Math.max(y3, y4));long m2 = Math.min(Math.max(x1, x2), Math.max(x3, x4));long n2 = Math.max(Math.min(y1, y2), Math.min(y3, y4));long sum = 0;if (m1 < m2 && n1 > n2)sum = (m1 - m2) * (n1 - n2);long res = Math.abs((y2 - y1) * (x2 - x1)) + Math.abs((y3 - y4) * (x3 - x4)) + sum;System.out.println(res);}
}
这篇关于矩形总面积(第十四届蓝桥杯JavaB组省赛真题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!