0

此类包含矩形列表,我需要找到面积最小的矩形。

我发现需要按区域比较矩形,但它具有双精度。我知道我的比较记住了最后一个,但是我们如何在这里进行检查呢?

代码:

   /**
    * Gets the Rectangle with the smallest area
    * @return the rectangle with the smallest area or null if
    * there are no rectangles
    */
   public Rectangle smallestArea()
   {        
       if (list.size() == 0) return null;

       Rectangle smallest = list.get(0);           
       double smallestArea = smallest.getWidth() * smallest.getHeight();

       for (int i = 1; i < list.size(); i++) {
           Rectangle next = list.get(i);
           double nextArea = next.getWidth() * next.getHeight();

           if ((nextArea - smallestArea) < 0) smallest = next;             
       }

       return smallest;
   }

如何解决这个问题?

4

7 回答 7

1

您还应该更新smallestArea局部变量(在您的代码中多写一行):

   public Rectangle smallestArea()
   {        
       if (list.size() == 0) return null;

       Rectangle smallest = list.get(0);           
       double smallestArea = smallest.getWidth() * smallest.getHeight();

       for (int i = 1; i < list.size(); i++) {
           Rectangle next = list.get(i);
           double nextArea = next.getWidth() * next.getHeight();

           if ((nextArea - smallestArea) < 0) { 
             smallest = next;         // <- Whenever you've updated smallest           
             smallestArea = nextArea; // <- Do not forget updating the smallestArea as well
           }  
       }

       return smallest;
   }
于 2013-08-30T07:14:03.187 回答
1

您的算法不起作用,因为您还需要更新smallestArea

if ((nextArea - smallestArea) < 0) {
    smallest = next; 
    smallestArea = nextArea; // <<== Here
}

请注意,这(nextArea - smallestArea) < 0是另一种说法nextArea < smallestArea,所以这看起来更干净:

if (nextArea < smallestArea) {
    smallest = next; 
    smallestArea = nextArea;
}
于 2013-08-30T07:14:16.650 回答
0

我会做

double smallest = Double.POSITIVE_INFINITY;

// in the loop.
if (smallest > next)
    smallest = next;
于 2013-08-30T07:13:12.027 回答
0

除了记住小矩形是什么之外,您还需要记住它的大小。更改if循环内部:

if (nextArea < smallestArea) {
    smallest = next;
    smallestArea = nextArea
}
于 2013-08-30T07:14:31.123 回答
0
public Rectangle smallestArea()
{        
   if (list.size() == 0) return null;

   Rectangle smallest = list.get(0);           
   double smallestArea = smallest.getWidth() * smallest.getHeight();

   for (int i = 1; i < list.size(); i++) {
       Rectangle next = list.get(i);
       double nextArea = next.getWidth() * next.getHeight();

       if ((nextArea - smallestArea) < 0){
          smallest = next;             
          smallestArea = nextArea;
       }
   }

   return smallest;
}
于 2013-08-30T07:14:44.547 回答
0

那这个呢?我想你可以试试这种方式

    List<Rectangle> list=new ArrayList<>();
    List<Double> areaList=new ArrayList<>();
    for(Rectangle r:list){
        areaList.add(r.getHeight()*r.getHeight());
    }
    Collections.sort(areaList);
    System.out.println("Smallest "+areaList.get(0));
于 2013-08-30T07:21:10.827 回答
-1

尝试,

int retval = Double.compare(nextArea, smallestArea);

if(retval < 0)
{
  System.out.println("smallestArea is greater than nextArea");
  smallest = next;
}
于 2013-08-30T07:16:43.710 回答