(请教)HDOJ2056(WA)(已解决)

本文介绍了一种通过排序和比较矩形顶点坐标来计算两个矩形交集面积的方法。该方法首先读取四个顶点坐标,计算并排序各点到原点的距离,接着通过比较确定是否相交及计算相交部分面积。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*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;

}

*/

//分离要单独考虑 不能单靠到原点距离判断 用点判断


  1. #include<stdio.h>   
  2. #include<algorithm>  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     int  i,j,k,n,m;  
  7.     double x[5],y[5],a[5],b[5],s;  
  8.     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)  
  9.     {  
  10.         s=0;  
  11.         for(i=1;i<=4;i++)  
  12.         {  
  13.             a[i]=x[i];  
  14.             b[i]=y[i];  
  15.         }//a[]、b[]数组记录原来数据   
  16.         sort(x+1,x+5);  
  17.         sort(y+1,y+5);  
  18.         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])   
  19.         ;                                                               //判断两个矩形没有相交  
  20.         else  
  21.         s=(x[3]-x[2])*(y[3]-y[2]);  
  22.         printf("%.2lf\n",s);  
  23.     }  
  24.       
  25.     return 0;  
  26. }  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值