您的位置:首页 > 其它

矩形相交的面积

2016-03-19 10:57 330 查看

杭电2056 Rectangles

[align=left]Problem Description[/align]
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 .

[align=left]Input[/align]
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).

[align=left]Output[/align]
Output For each case output the area of their intersected part in a single line.accurate up to 2 decimal places.

[align=left]Sample Input[/align]

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


[align=left]Sample Output[/align]

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;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: