【思路】:将各个边平行到x、y轴上,重叠部分相乘得面积。getLen中是两条线相交的各种判断。注意:因为输入一个矩形的两个点并没有说先输入左下再输入右上(测试数据也确实有先输入了右上),所以一定要两两排序。否则会出现a大于b的情况。
【AC代码】:
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <cstdio>
#include <cstring>
using namespace std;
double getLen(double x[])
{
double a = x[0], b = x[1], c = x[2], d = x[3];
if (b < c)
{
return 0.0;
}
else if (b >= c && b <= d)
{
if (a > c)
return b-a;
else
return b-c;
}
else if (b > d)
{
if (a <= c)
return d-c;
else if (a > c && a < d)
return d-a;
else
return 0.0;
}
}
int main()
{
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
double x[4], y[4];
int i = 0;
//input
for (i = 0; i < 4; i++)
{
cin >> x[i] >> y[i];
}
//get len
sort(x, x+2);
sort(x+2, x+4);
sort(y, y+2);
sort(y+2, y+4);
double len_x = getLen(x);
double len_y = getLen(y);
//cout << len_x << " " << len_y << endl;
cout << fixed << setprecision(2) << len_x*len_y;
}原文地址:http://blog.csdn.net/weijj6608/article/details/44514453