1.纯暴力白嫖分
java
import java.util.*;
public class Main {
public static void main (String []args) {
Scanner a = new Scanner(System.in);
int x1,x2,x3,x4,y1,y2,y3,y4;
x1 =a.nextInt();
y1 =a.nextInt();
x2 =a.nextInt();
y2 =a.nextInt();
x3 =a.nextInt();
y3 =a.nextInt();
x4 =a.nextInt();
y4 =a.nextInt();
int area1 = (x2-x1)*(y2-y1);
int area2 = (x4-x3)*(y4-y3);
System.out.println(area1+area2);
}
}
2.穷举(我发现情况有点多穷举不完大概有个至少10种我得拿张草稿纸验算一下)
java
import java.util.*;
public class Main {
public static void main (String []args) {
Scanner a = new Scanner(System.in);
long x1,x2,x3,x4,y1,y2,y3,y4;
long area1,area2,n;
long area3;//重合部分
x1 =a.nextLong();
y1 =a.nextLong();
x2 =a.nextLong();
y2 =a.nextLong();
x3 =a.nextLong();
y3 =a.nextLong();
x4 =a.nextLong();
y4 =a.nextLong();
area1 = (x2-x1)*(y2-y1);
area2 = (x4-x3)*(y4-y3);
long a1 = Math.max(x1, x3);
long a2 = Math.min(x2, x4);
long b1 = Math.max(y1, y3);
long b2 = Math.min(y2, y4);
long n1 = area1+area2-(a2-a1)*(b2-b1);
// //两个矩形不重合且不包含
// if(x3>x2&&y3>y2) {
// n = area1+area2;
// }
// else if(x4<x1&&y4<y1){
// n = area1+area2;
// }
// else if(x3<x2 && x3>x1 && x4>x2 && y3>y1 && y3<y2 && y4>y2) {
// n = area1+area2-(x2-x3)*(y2-y3);
// }
// else if(x4<x2 && x4>x1 && x3<x1 && y4>y1 && y4<y2 && y3<y1) {
// n = area1+area2-(x4-x1)*(y4-y1);
// }
// else if(x3<x2 && x3>x1 && x4>x2 && y3<y1 && y4<y2 && y4>y1) {
// n = area1+area2-(x4-x1)*(y4-y1);
// }
// else if(x4<x2 && x4>x1 && x3<x1 && y4>y1 && y4<y2 && y3<y1) {
// n = area1+area2-(x4-x1)*(y4-y1);
// }
// else {
// if(area1>area2) {
// n = area1;
// }
// else
// n = area2;
// }
System.out.println(n1);
}
}
3.巧法
java
import java.util.*;
public class Main {
public static void main (String []args) {
Scanner a = new Scanner(System.in);
long x1,x2,x3,x4,y1,y2,y3,y4;
long area1,area2,n;
long area3;//重合部分
x1 =a.nextLong();
y1 =a.nextLong();
x2 =a.nextLong();
y2 =a.nextLong();
x3 =a.nextLong();
y3 =a.nextLong();
x4 =a.nextLong();
y4 =a.nextLong();
area1 = (x2-x1)*(y2-y1);
area2 = (x4-x3)*(y4-y3);
long a1 = Math.max(x1, x3);
long a2 = Math.min(x2, x4);
long b1 = Math.max(y1, y3);
long b2 = Math.min(y2, y4);
long n1 = area1+area2-(a2-a1)*(b2-b1);
System.out.println(n1);
}
}