IoU

题目链接https://vjudge.net/contest/241657#problem/G

 

题目大意

求两个矩形的交集/并集

 

解题关键

找到交集的面积,并集可用两个矩形面积和减去交集面积得到

int width=min(x1+w1,x2+w2)-max(x1, x2);
int height=min(y1+h1, y2+h2)-max(y1, y2);

 

 

ac代码

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;

int main()
{
    int t;
    cin >> t;
    int x1,x2,y1,y2,h1,h2,w1,w2;
    while(t--){
        scanf("%d %d %d %d",&x1,&y1,&w1,&h1);
        scanf("%d %d %d %d",&x2,&y2,&w2,&h2);

        if(w1==0||w2==0||h1==0||h2==0){
            printf("0.00\n");
        }else{
            int width=min(x1+w1,x2+w2)-max(x1, x2);
            int height=min(y1+h1, y2+h2)-max(y1, y2);
            if(width<=0||height<=0){
                printf("0.00\n");
            }else{
                double sj=width*height;
                double us=w1*h1+w2*h2-sj;
                printf("%.2f\n", sj/us);
            }
        }
    }
    return 0;
}

 

posted @ 2018-07-29 23:34  kangrrrr  阅读(68)  评论(0)    收藏  举报