2020.5.10
学习的第20天咯
好习惯是多少天养成来着~~
题目:求两个矩形相交的面积
C 代码
#include<stdio.h>
typedef struct {
double x, y;
}point;
double max(double a, double b);
double min(double a, double b);
int main()
{
point p[4];
double x1, y1, x2, y2;
for (int i = 0; i < 4; i++)
scanf("%lf%lf", &p[i].x, &p[i].y);
x1 = min(max(p[0].x, p[1].x), max(p[2].x, p[3].x));
y1 = min(max(p[0].y, p[1].y), max(p[2].y, p[3].y));
x2 = max(min(p[0].x, p[1].x), min(p[2].x, p[3].x));
y2 = max(min(p[0].y, p[1].y), min(p[2].y, p[3].y));
if ((p[0].x != p[1].x && p[2].x != p[3].x) && (p[0].y != p[1].y && p[2].y != p[3].y))
//用来判断输入点的坐标是否能构成矩形
if (x1 > x2 && y1 > y2)
//当两个矩形相交或者包含时
printf("%.2lf", (x1 - x2) * (y1 - y2));
else
//两个矩形不相交,面积为0
printf("0.00");
else
printf("0.00");
return 0;
}
double max(double a, double b)
{
return a > b ? a : b;
}
double min(double a, double b)
{
return a > b ? b : a;
}
Java 代码
import java.util.Scanner;
class point {
double x, y;
point(double x, double y) {
this.x = x;
this.y = y;
}
}
public class Rectangle_Intersect_S {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
point p[] = new point[4];
for (int i = 0; i < 4; i++) {
p[i] = new point(input.nextInt(), input.nextInt());
}
Rectangle_Intersect_S.s(p);
}
public static void s(point p[]) {
double x1, x2, y1, y2;
x1 = Math.min(Math.max(p[0].x, p[1].x), Math.max(p[2].x, p[3].x));
y1 = Math.min(Math.max(p[0].y, p[1].y), Math.max(p[2].y, p[3].y));
x2 = Math.max(Math.min(p[0].x, p[1].x), Math.min(p[2].x, p[3].x));
y2 = Math.max(Math.min(p[0].y, p[1].y), Math.min(p[2].y, p[3].y));
if ((p[0].x != p[1].x && p[2].x != p[3].x) && (p[0].y != p[1].y && p[2].y != p[3].y))
if (x1 > x2 && y1 > y2)
System.out.println(String.format("%.2f", (x1 - x2) * (y1 - y2)));
else
System.out.println("0.00");
else
System.out.println("0.00");
}
}
昨天写了,一下有事忘记发出来了 = = 罪过罪过
算法题目来源:https://www.dotcpp.com/