• 矩形(两个矩形覆盖面积)


    1816: 矩形

    Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 93  Solved: 25
    SubmitStatusWeb Board

    Description

    小S和小D两个小雪参,在玩一个关于矩形的游戏以帮助他们学会怎么计算矩形的面积然后完成作业去打游戏(两个小雪参一起打游戏你怕不怕!)。他们约定每个人给出两个坐标,分别是两个矩形的左下角和右上角的坐标,他们的游戏规则很简单,就是看谁先找出来这两个矩形的覆盖的总面积大小。你的问题就是帮助他们给出正确答案。

    Input

    输入的第一行为一个整数T,代表样例的组数。

    每组样例包含4个坐标(x,y),( -100000000<= x,y <= 100000000 )前两个是小S给出的矩形左下角和右上角的坐标,后面两个是小D给出来的坐标。

    Output

    输出两个矩形覆盖的总面积大小。

    Sample Input

    3 -2 -2 2 2 -2 -2 2 2 0 0 1 1 0 0 2 2 -2 -2 2 2 1 -3 3 -1

    Sample Output

    16 4 19

    HINT

    题解:有一吊人名曰程林浩,聪明伶俐,如此聪明。。。佩服之至啊;

    这个覆盖面积只需要把左右覆盖长度相加,减去区间宽度就好了,不想交自然小于零,画个图想下就好,思路很巧妙;

    自己神经的也想模拟下。。。果断错。。。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<map>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const double PI=acos(-1.0);
    typedef long long LL;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define PI(x) printf("%d",x)
    #define PL(x) printf("%lld",x)
    #define SI(x) scanf("%d",&x)
    #define SL(x) scanf("%lld",&x)
    #define P_ printf(" ")
    #define T_T while(T--)
    double a[4],b[4];
    int main(){
    	int T;
    	double ax,ay,bx,by,Ax,Ay,Bx,By;
    	SI(T);
    	T_T{
    		scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&ax,&ay,&bx,&by,&Ax,&Ay,&Bx,&By);
    		a[0]=ax;a[1]=bx;a[2]=Ax;a[3]=Bx;
    		b[0]=ay;b[1]=by;b[2]=Ay;b[3]=By;
    		double w=*max_element(a,a+4)-*min_element(a,a+4),h=*max_element(b,b+4)-*min_element(b,b+4);
    		double x=abs(bx-ax)+abs(Ax-Bx)-w,y=abs(by-ay)+abs(Ay-By)-h;
    		double s=abs(bx-ax)*abs(by-ay)+abs(Ax-Bx)*abs(Ay-By);
    		if(x<=0||y<=0)printf("%.0lf
    ",s);
    		else printf("%.0lf
    ",s-x*y);
    	}
    	return 0;
    }
    

      自己神经的模拟,模拟了了一半,果断放弃。。。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #include<stack>
    #include<map>
    using namespace std;
    const int INF=0x3f3f3f3f;
    const double PI=acos(-1.0);
    typedef long long LL;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define PI(x) printf("%d",x)
    #define PL(x) printf("%lld",x)
    #define SI(x) scanf("%d",&x)
    #define SL(x) scanf("%lld",&x)
    #define P_ printf(" ")
    #define T_T while(T--)
    
    int main(){
    	int T;
    	int ax,ay,bx,by,Ax,Ay,Bx,By;
    	SI(T);
    	T_T{
    		scanf("%d%d%d%d%d%d%d%d",&ax,&ay,&bx,&by,&Ax,&Ay,&Bx,&By);
    		int temp=ay;ay=by;by=temp;
    		temp=Ay;Ay=By;By=temp;
    		int x,y;
    		//
    		if(ax>=Ax){
    			if(bx>=Ax){
    				x=0;
    			}
    			else if(bx<Ax){
    				if(bx>=Bx)x=Ax-bx;
    				else x=Ax-Bx;
    			}
    		}
    		else if(ax<Ax){
    			if(ax>=Bx){
    				if(bx>=Bx)x=ax-bx;
    				else x=ax-Bx;
    			}
    			else if(ax<Bx){
    				x=0;
    			}
    		}
    		//
    		if(ay>=Ay){
    			if(by>=Ay){
    				y=0;
    			}
    			else if(by<Ay){
    				if(by>=By)y=Ay-by;
    				else y=Ay-By;
    			}
    		}
    		else if(ay<Ay){
    			if(ay>=By){
    				if(by>=By)y=ay-by;
    				else y=ay-By;
    			}
    			else if(ay<By){
    				y=0;
    			}
    		}
    		int s=(ay-by)*(bx-ax)+(Ay-By)*(Bx-Ax);
    		printf("%d
    ",s-x*y);
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    MD5 加密 以及 加盐加密
    github--新手使用错误分析
    iOS开发UI篇—UITabBarController简单介绍
    Xcode 那些简单实用的插件推荐
    app 转caf 音频 代码
    关于block 用法
    iOS 之播放系统声音
    ios如何实现推送通知
    Centos7安装Redis
    SpringCloud-Zuul网关
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5031193.html
Copyright © 2020-2023  润新知