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

  • 问题描述
  • 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.Cai\'s Field_第1张图片
 所以,我们只需在两个矩形中,取左边横坐标大的,右边横坐标小的,上边纵坐标小的,下边纵坐标大的,得到的就是相交的部分,上图是一种情况,其他情况也类似,当然如果取出来的坐标,左边>右边或者上边<下边的话,就说明没有交集,值得注意的是,输入的坐标可能是主对角线的,也可能是副对角线,所以要处理交换下。

#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;
}

你可能感兴趣的:(NOJ——[1234] Mr.Cai\'s Field)