NOJ——[1234] Mr.Cai\'s Field

本文介绍如何通过编程计算两个矩形之间的相交面积,包括输入解析、边界处理及输出格式化。

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

  • 问题描述
  • Mr.Cai is a peasant who owns some field,but due to his neglect,he earned little this year.Now autumn gradually away

    from us,it is time to hand in taxs again,there are many exorbitant taxes and levies.After knowing Mr.Cai's poor condition ,

    the landlord named "Zhou Ba Pi" said:"It's fine,just give me your field as hypothecate,since you must work on it to earn

    money for me ,I won't take them all,I'll set a rule to show how will I take apart these field".

    The rule is described as follow:give you a rectangle,means Mr.Cai's field,then give you another rectangle,if this two rectangles overlaped,Zhou will take this part away,so your task is to calculate the area of overlaped part.To make it easier,we confirm that every side is vertical or horizontal.

  • 输入
  • There are multiple test cases.Every case include 8 real numbers,each with two decimal point,a1,b1,a2,b2,a3,b3,a4 b4,first rectangle is (a1,b1) - (a2,b2),another is(a3,b3) - (a4,b4).
    Look at Sample input,it's explain is like the picture shown above.
  • 输出
  • Each case output the area of overlaped part in a single line,and keep two decimal point too.
  • 样例输入
  • 1.00 6.00 3.00 4.00 2.00 5.00 4.00 2.00
    -1.00 -1.00 1.00 1.00 2.00 2.00 3.00 3.00
  • 样例输出
  • 1.00
    0.00
  • 提示
  • 来源
  • Mr.Cai
求矩形相交面积。
NOJ——[1234] Mr.Cais Field - 深海灬孤独 - Alex
 所以,我们只需在两个矩形中,取左边横坐标大的,右边横坐标小的,上边纵坐标小的,下边纵坐标大的,得到的就是相交的部分,上图是一种情况,其他情况也类似,当然如果取出来的坐标,左边>右边或者上边<下边的话,就说明没有交集,值得注意的是,输入的坐标可能是主对角线的,也可能是副对角线,所以要处理交换下。

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
double x1,x2,y1,y2,x3,x4,y3,y4;
while(~scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2))
{
scanf("%lf%lf%lf%lf",&x3,&y3,&x4,&y4);
if(x1>x2)
swap(x1,x2);
if(y1>y2)
swap(y1,y2);
if(x3>x4)
swap(x3,x4);
if(y3>y4)
swap(y3,y4);
double lx=max(x1,x3);
double rx=min(x2,x4);
double ly=min(y4,y2);
double ry=max(y1,y3);
if(lx > rx || ly < ry )
printf("0.00\n");
else
printf("%.2f\n",(rx-lx)*(ly-ry));
}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值