问题描述
平面上有两个矩形,它们的边平行于直角坐标系的X轴或Y轴。对于每个矩形,我们给出它的一对相对顶点的坐标,请你编程算出两个矩形的交的面积。
输入格式
输入仅包含两行,每行描述一个矩形。
在每行中,给出矩形的一对相对顶点的坐标,每个点的坐标都用两个绝对值不超过10^7的实数表示。
输出格式
输出仅包含一个实数,为交的面积,保留到小数后两位。
样例输入
1 1 3 3
2 2 4 4
样例输出
1.00
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
double x[4];
double y[4];
//freopen("input7.txt","r",stdin);
for(int i = 0; i < 4; i++)
scanf("%lf%lf", &x[i], &y[i]);
double m1 = max(x[0], x[1]), n1 = min(y[0], y[1]);
double m2 = max(x[2], x[3]), n2 = min(y[2], y[3]);
double n3 = max(y[0], y[1]), m3 = min(x[0], x[1]);
double n4 = max(y[2], y[3]), m4 = min(x[2], x[3]);
if(m1 <= m4 || m3 >= m2 || n1 >= n4 || n3 <= n2)
printf("0.00\n");
else
{
sort(x, x + 4);
sort(y, y + 4);
printf("%.2lf\n", (x[2] - x[1]) * (y[2] - y[1]));
}
return 0;
}
这个没有用using namespace std。。。貌似是不用的好一点吧 , 就是麻烦
#include<cstdio>
#include<cmath>
#include<algorithm>
int main()
{
double x[5], y[5];
int i;
for(i=1; i<=4; i++)
scanf("%lf%lf", &x[i], &y[i]);
if((std::max(x[1], x[2])<std::min(x[3], x[4])) || (std::max(x[3], x[4])<std::min(x[1], x[2])) || (std::max(y[1], y[2])<std::min(y[3], y[4])) || (std::max(y[3], y[4])<std::min(y[1], y[2])))
printf("0.00\n");
else{
std::sort(x+1, x+5);
std::sort(y+1, y+5);
printf("%.2lf\n", (x[3]-x[2])*(y[3]-y[2]));
}
return 0;
}
下面这个仅有42分, 有四个例子显示运行错误,但还是感觉这个和CCF第二题很像
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
double n=2, a, b, c, d, cnt = 0, all=0, a1, b1, c1, d1, e, f, g, h, e1, f1, g1, h1, M, N;
// cin >> n;
// while(n--)
{
cin >> a >> b >> c >> d >> e >>f >> g >> h;
a1 = min(a, c);
c1 = max(a, c);
b1 = min(b, d);
d1 = max(b, d);
e1 = min(e, g);
g1 = max(e, g);
f1 = min(f, h);
h1 = max(f, h);
M = max(c1, g1);
N = max(d1, h1);
int flag[(int)M][(int)N] = {0};
all = (d1-b1)*(c1-a1) + (h1-f1)*(g1-e1);
for(int i=a1; i<c1; i++)
{
for(int j=b1; j<d1; j++)
{
if(flag[i][j] == 0)
cnt++;
flag[i][j]++;
}
}
for(int i=e1; i<g1; i++)
{
for(int j=f1; j<h1; j++)
{
if(flag[i][j] == 0)
cnt++;
flag[i][j]++;
}
}
}
// cout << cnt;
cout << fixed << setprecision(2) << (all-cnt) << endl;
return 0;
}