/*WA思路:
先对点做标记 前两个是1 后两个是2
然后对四个点进行排序交换(排序条件:到原点的距离 小到大)
如果排序后前两个的标记一样(没有公共面积)输出0.00
否则 计算a[1] a[2]间的面积
感觉其他博客写的都很繁琐 想写个简便的 却一直WA
问题应该是出在排序上 但感觉不到错在哪
#include <iostream>
#include <cstring>#include <string>
#include <cstdio>
#include <algorithm>
#include <stack>
#include <math.h>
#include<iostream>
using namespace std;
struct aa
{
double x;
double y;
double z;
int f;
}a[4];
bool cmp(aa a, aa b)
{
return a.z<b.z;
}
int main()
{
while (cin >> a[0].x >> a[0].y)
{
a[0].f = 1;
a[0].z = sqrt(pow(a[0].x, 2) + pow(a[0].y, 2));
for (int i = 1; i < 4; i++)
{
cin >> a[i].x >> a[i].y;
a[i].z = sqrt(pow(a[i].x, 2) + pow(a[i].y, 2));
if (i == 1) a[i].f = 1;
else a[i].f = 2;
}
sort(a, a + 4, cmp);
if (a[0].f == a[1].f) cout <<"0.00" << endl; //前两个点都属于同一个长方形 就没有交集
else
{
double area = (a[2].x - a[1].x)*(a[2].y - a[1].y);
printf("%.2lf\n", area);
}
}
return 0;
}
*/
//分离要单独考虑 不能单靠到原点距离判断 用点判断
- #include<stdio.h>
- #include<algorithm>
- using namespace std;
- int main()
- {
- int i,j,k,n,m;
- double x[5],y[5],a[5],b[5],s;
- while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x[1],&y[1],&x[2],&y[2],&x[3],&y[3],&x[4],&y[4])==8)
- {
- s=0;
- for(i=1;i<=4;i++)
- {
- a[i]=x[i];
- b[i]=y[i];
- }//a[]、b[]数组记录原来数据
- sort(x+1,x+5);
- sort(y+1,y+5);
- if(x[3]>a[2]&&x[3]>a[1]||x[3]>a[3]&&x[3]>a[4]||y[3]>b[1]&&y[3]>b[2]||y[3]>b[3]&&y[3]>b[4])
- ; //判断两个矩形没有相交
- else
- s=(x[3]-x[2])*(y[3]-y[2]);
- printf("%.2lf\n",s);
- }
- return 0;
- }