Rectangles

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20375    Accepted Submission(s):
6607

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

import java.util.Scanner;

class Main{

public static void main(String[] args) {

Scanner cin=new Scanner(System.in);
while(cin.hasNext()){
double t;
double sum=0.0;
double x1=cin.nextDouble();
double y1=cin.nextDouble();
double x2=cin.nextDouble();
double y2=cin.nextDouble();
double x3=cin.nextDouble();
double y3=cin.nextDouble();
double x4=cin.nextDouble();
double y4=cin.nextDouble();
if(x1>x2){t=x1;x1=x2;x2=t;}
if(y1>y2){t=y1;y1=y2;y2=t;}
if(x3>x4){t=x3;x3=x4;x4=t;}
if(y3>y4){t=y3;y3=y4;y4=t;}
x1=max(x1,x3);
x2=min(x2,x4);
y1=max(y1,y3);
y2=min(y2,y4);
if(x1>x2||y1>y2){
System.out.println("0.00");
}
else{
sum=(x2-x1)*(y2-y1);
System.out.printf("%.2f",sum);
System.out.println();
}

}
}
private static double min(double x2, double x4) {
if(x2<x4)
return x2;
else
return x4;
}
private static double max(double x1, double x3) {
if(x1>x3)
return x1;
else
return x3;
}
}

这个地方要注意的是的判断一下图形是否相交,如果不相交,还是要输出按题目中的输出要求:0.00;不然就会出错,这个题目只有相交和分离两种状态;

HDU2056JAVA的更多相关文章

随机推荐

  1. App小样在手机运行了一下

    外包公司把App小样的安装包发过来了,我在安卓手机上试了一把,虽然还只有几个静态页面,但安装那一刻还是小激动了一把. 在某美术系MM的帮助下,我基本掌握了原型软件azure. 事实证明,很多东西都是逼 ...

  2. SecureCRT 绝佳配色方案, 保护你的眼睛

    http://blog.csdn.net/zklth/article/details/8937905   关键词:SecureCRT配色, SecureCRT设置颜色, Linux终端配色,Linux ...

  3. NetFlow网络流量监测技术的应用和设计(转载)

    http://blog.chinaunix.net/uid-20466300-id-1672909.html http://www.cww.net.cn/news/html/2014/12/25/20 ...

  4. 【网络流24题】No. 13 星际转移问题 (网络判定 最大流)

    [题意] 由于人类对自然资源的消耗, 人们意识到大约在 2300 年之后, 地球就不能再居住了.于是在月球上建立了新的绿地,以便在需要时移民. 令人意想不到的是, 2177 年冬由于未知的原因, 地球 ...

  5. 心愿:做一个精简版MFC

    我觉得自己有能力看懂MFC的C++代码和总体流程,但是由于MFC混杂了太多的东西,比如OLE等等不必要的东西,又做了无数的ASSERT判断,影响整体流程的理解.因此我要做一个精简版的MFC,而且能用它 ...

  6. HDU-1874 畅通工程续 (最短路径启蒙题)

    hdu 1874比较基础,拿来练各种刚学会的算法比较好,可以避免好多陷阱,典型的最短路模板题 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memor ...

  7. LRU 缓冲池 (不考虑多线程)

    lru:(转)LRU算法的实现 什么是LRU算法? LRU是Least Recently Used的缩写,即最近最少使用页面置换算法,是为虚拟页式存储管理服务的.关于操作系统的内存管理,如何节省利用容 ...

  8. 最短路SPFA

    用邻接矩阵a表示一幅图,a[i][j]表示从点i到点j的边长,如果为0则无边.(这是无负边,0边的情况) 这张图有T个点,C条边,要求求出从Ts走到Te的最短路. 用f[i]表示从Ts走到i点的最短路 ...

  9. Android NDK开发Crash错误定位[转]

    使用 ndk-stack 的时候需要你的 lib 编译为 debug版的,通常需要下面的修改: 1. 修改 android.mk,增加,为 LOCAL_CFLAGS 增加 -g 选项 2. 修改 ap ...

  10. mongDB基本命令和Java操作MongoDB

    上一篇博文<mongoDB安装>我们安装了mongoDB,现在来复习一下它的一些基本命令:mongoDB的bin目录加入到path之后,命令行中输入mongo: 然后我们进入正题 1.查看 ...