[Java] 杭电ACM 2056 Rectangles,求相交矩形的面积 Java实现 →→→→→进入此内容的聊天室

来自 , 2020-08-20, 写在 Java, 查看 131 次.
URL http://www.code666.cn/view/67baaa05
  1. import java.io.*;
  2. import java.text.DecimalFormat;
  3. import java.util.*;
  4. public class Main {
  5.         public static void main(String[] args) {
  6.                 Scanner sc=new Scanner(new BufferedInputStream(System.in));
  7.                 while(sc.hasNextDouble()){
  8.                         double s=0;
  9.                         double res[]=new double[4];
  10.                         double res1[]=new double[4];
  11.                         double res2[]=new double[4];
  12.                         double point1[]=new double[4];
  13.                         double point2[]=new double[4];
  14.                         for(int i=0;i<point1.length;i++){
  15.                                 point1[i]=sc.nextDouble();
  16.                         }
  17.                         for(int i=0;i<point2.length;i++){
  18.                                 point2[i]=sc.nextDouble();
  19.                         }
  20.                         //  第一个矩形左上角的坐标
  21.                         res1[0]=Math.min(point1[0], point1[2]);//X1
  22.                         res1[1]=Math.max(point1[1], point1[3]);//Y1
  23.                         //第一个矩形右下角的坐标
  24.                         res1[2]=Math.max(point1[0], point1[2]);//x2
  25.                         res1[3]=Math.min(point1[1], point1[3]);//y2
  26.                         //  第二个矩形左上角的坐标
  27.                         res2[0]=Math.min(point2[0], point2[2]);//x1
  28.                         res2[1]=Math.max(point2[1], point2[3]);//y1
  29.                         // 第二个矩形右下角的坐标
  30.                         res2[2]=Math.max(point2[0], point2[2]);//x2
  31.                         res2[3]=Math.min(point2[1], point2[3]);//y2
  32.                         // 如果矩形重合和相离  s=0  
  33.                         if(res2[0]>=res1[2]||res2[2]<=res1[0]||res2[3]>=res1[1]||res2[1]<=res1[3]){
  34.                                 s=0;
  35.                         }
  36.                         else{
  37.                                 //否者矩形相交的 坐标
  38.                                 res[0]=Math.max(res1[0], res2[0]);
  39.                                 res[1]=Math.min(res1[1],res2[1]);
  40.                                 res[2]=Math.min(res1[2],res2[2]);
  41.                                 res[3]=Math.max(res1[3],res2[3]);
  42.                                 s=(res[2]-res[0])*(res[1]-res[3]);
  43.                         }
  44.                         DecimalFormat fo=new DecimalFormat("0.00");
  45.                         System.out.println(fo.format(s));
  46.                 }
  47.         }
  48.  
  49. }
  50.  
  51.  
  52. //java/8358

回复 "杭电ACM 2056 Rectangles,求相交矩形的面积 Java实现"

这儿你可以回复上面这条便签

captcha