矩形相交的面积

杭电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 of input is 8 positive numbers which indicate the coordinates of four points that must be on each diagonal.The 8 numbers are x1,y1,x2,y2,x3,y3,x4,y4.That means the two points on the first rectangle are(x1,y1),(x2,y2);the other two points on the second rectangle are (x3,y3),(x4,y4).
 

Output
Output For each case output the area of their intersected part in a single line.accurate up to 2 decimal places.
 

Sample Input
   
   
1.00 1.00 3.00 3.00 2.00 2.00 4.00 4.00 5.00 5.00 13.00 13.00 4.00 4.00 12.50 12.50
 

Sample Output
   
   
1.00 56.25
题目意思就是输入两个矩形的对角线两点的坐标,求两矩形相交的面积;

1;这是一个几何水题,但是初次遇几何题的我却做了好久好久,最开始是去分情况后来觉得情况太多了;在思考了一下发现可以先排序则相交面积一定是排序x第二的减去第三的再乘以y第二的减去第三的;他们的乘积就是相交的面积;然而是不是总是wa呢;

2;考虑情况要全面,做几何题就是要考虑特殊情况列如该题就要考虑没有相交的情况(并且要输出0.00)保留2位小数;然而可能还是wa呢;

3;先看一道很水很水的题目吧;杭电2002计算球的体积;

Problem Description

根据输入的半径值,计算球的体积。

 

Input

输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。

 

Output

输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。

 

Sample Input

1

1.5

 

Sample Output

4.189

14.137

很简单吧,但你们有没有注意如果定义变量r为flaot单精度形时提交是wa;但把r的定义改成double时就ac了;杭电就是这么神奇的;因为它的后台数据存在这种情况,如果定义flaot就会出错;同样的这题也是这样把输入的变量改为double型就ac了;

4;看代码吧;

#include<stdio.h>

#include<math.h>

double max(floata,float b)

{

    return (a>b?a:b);

}

double min(floata,float b)

{

    return (a<b?a:b);

}

int main()

{

       double x[4], y[4];

       double a, b, c, d, e, f, g, h;

       int i, j;

       double temp;

       while(scanf("%lf %lf %lf %lf %lf %lf%lf %lf",&x[0],&y[0],&x[1],&y[1],&x[2],&y[2],&x[3],&y[3])!=EOF)

       {

       a=max(x[0],x[1]);

       b=min(x[0],x[1]);

       c=max(y[0],y[1]);

     d=min(y[0],y[1]);

 

     e=max(x[2],x[3]);

     f=min(x[2],x[3]);

     g=max(y[2],y[3]);

     h=min(y[2],y[3]);

    if(a<f||b>e||c<h||d>g)//a<f||b>e矩形2在矩形1的两边;c<h||d>g 形2在矩形1的上下

    {

              printf("0.00\n");

              continue;

              }

      for(i=0;i<3;i++)//冒泡排序;

        for(j=0;j<3-i;j++)

        {

                  if(x[j]>x[j+1])

                  {

                      temp=x[j];

                      x[j]=x[j+1];

                      x[j+1]=temp;

                  }

            if(y[j]>y[j+1])

            {

                temp=y[j];

                y[j]=y[j+1];

                y[j+1]=temp;

            }

        }

       

        printf("%.2lf\n",(x[2]-x[1])*(y[2]-y[1]));

       }

       return 0;

}

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值