求两个矩形的重叠面积

巧妙:通过边的相对位置和大小进行计算

#include

double max(double a, double b)
{
	if(a>b) return a;
	return b;
 } 
 
double min(double a, double b)
{
	if(a>b) return b;
	return a;
 } 

 
int main()
{
    double a,b,c,d,e,f,g,h;
    double s;
    while(scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f,&g,&h)!=EOF)
    {
    	s = 0.00;
    	double x1,y1,x2,y2,x3,y3,x4,y4;
    	
		x1 = min(a,c); x2 = max(a,c);       //确保对角线的点是左下和右上
    	x3 = min(e,g); x4 = max(e,g);
    	y1 = min(b,d); y2 = max(b,d);
    	y3 = min(f,h); y4 = max(f,h);
    
    	double left = max(x1,x3);
    	double right = min(x2,x4);
    	double up = min(y2,y4);
    	double down = max(y1,y3);
    	//printf("%f %f %f %f\n",left,right,up,down);
    	if(left < right && up > down) s = (right-left)*(up-down);
    	printf("%.2lf\n",s);
	} 
    
	
	return 0;
}

你可能感兴趣的:(厦大ACM入门百练,c#,算法,c算法)