HDU 2056 Rectangles

这题就是简单的几何题,刚接触ACM做这题时,不会写,当时想的太复杂了,把矩形的各种情况组合都考虑到了,结果发现这样太复杂就放弃了。今天做这道题时,我突然发现既然题目给的是对角线的坐标,为什么不用对角线之间的关系来判别矩形之间的位置关系呢?于是思路就很简单的涌现出来了。只要画个图,就能明白两者之间对角线的关系。

#include<cstdio>
#include<cstring>
#include<algorithm>
double _max(double x,double y)
{
    if(x>y)
        return x;
    return y;
}
double _min(double x,double y)
{
    if(x<y)
        return x;
    return y;
}
using namespace std;
int main()
{
    double x1,y1,x2,y2,x3,y3,x4,y4,area;
    while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4)!=EOF)
    {
        double _x1=_min(x1,x2),_x2=_max(x1,x2),_y1=_min(y1,y2),_y2=_max(y1,y2);//分别求出矩形对角线两个点的x,y的最大最小值
        double _x3=_min(x3,x4),_x4=_max(x3,x4),_y3=_min(y3,y4),_y4=_max(y3,y4);
        x1=_max(_x1,_x3),x2=_min(_x2,_x4);//把两条对角线进行比较
        y1=_max(_y1,_y3),y2=_min(_y2,_y4);
        //printf("x1=%lf,x2=%lf,y1=%lf,y2=%lf\n",x1,x2,y1,y2);
        if(x1>x2||y1>y2)
            area=0.0;
        else
        {
            double a=x2-x1,b=y2-y1;
            area=a*b;
        }
        printf("%.2lf\n",area);
    }
    return 0;
}
时间: 2024-10-16 09:52:56

HDU 2056 Rectangles的相关文章

HDU 2056 Rectangles(计算相交面积)

Rectangles Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 20183    Accepted Submission(s): 6537 Problem Description Given two rectangles and the coordinates of two points on the diagonals of e

[容斥原理] hdu 2461 Rectangles

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2461 Rectangles Time Limit: 5000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1268    Accepted Submission(s): 665 Problem Description You are developing a soft

HDU 2461 Rectangles#容斥原理

http://acm.hdu.edu.cn/showproblem.php?pid=2461 题目很简单,但是由于询问数M可以很大,所以容易超时,这道题学到了在结构体里面写函数的方法,这样子效率更高,否则的话,这道题就TLE了. 根据容斥原理,先把每个小长方形的面积加上,然后看有没有与该小长方形相交的,用dfs实现,当相交面积为0时,则不进行dfs,且同样遵循奇加偶减(但代码里因为是以第二个作为depth=1开始进行dfs的,所以是奇减偶加). AC代码: #include<iostream>

HDoj 2056 Rectangles

Problem Description Given two rectangles and the coordinates of two points on the diagonals of each rectangle,you have to calculate the area of the intersected part of two rectangles. its sides are parallel to OX and OY . Input Input The first line o

hdu 2056

ps:    - -惭愧...是套用一个大神的计算方法来做的.....下面是代码 代码: #include "stdio.h"#include "stdlib.h"#include "math.h"int cmp(const void *a,const void *b){ return *(double *)a>*(double *)b?1:-1;}int main(){ int n=1,i; double s,l,h,x[4],y[4],

杭电oj2047-2049、2051-2053、2056、2058

2047  阿牛的EOF牛肉串 1 #include<stdio.h> 2 3 int main(){ 4 int n,i; 5 _int64 s[51]; 6 while(~scanf("%d",&n)){ 7 s[1]=3;s[2]=8; 8 for(i=3;i<=n;i++){ 9 s[i] = s[i-1]*2 + s[i-2]*2; 10 } 11 printf("%I64d\n",s[n]); 12 } 13 14 15 } 2

HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1506 Appoint description: Description A histogram is a polygon composed of a sequence of rectangles aligned a

HDU分类

模拟题, 枚举 1002 1004 1013 1015 1017 1020 1022 1029 1031 1033 1034 1035 1036 1037 1039 1042 1047 1048 1049 1050 1057 1062 1063 1064 1070 1073 1075 1082 1083 1084 1088 1106 1107 1113 1117 1119 1128 1129 1144 1148 1157 1161 1170 1172 1177 1197 1200 1201 12

HDU 1506 Largest Rectangle in a Histogram

Largest Rectangle in a Histogram Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 150664-bit integer IO format: %I64d      Java class name: Main A histogram is a polygon composed of a sequence of rectangles al