找到三个整数中的最大值并知道选择哪一个

时间:2016-05-25 08:51:09

标签: java max

我目前正在研究一个必须找到最佳解决方案的代码。首先,我检查三者中的一个是否大于另外两个。因此,最大值只出现一次。如果有两个数字大于第三个数字,但彼此相等,我将不得不比较这两个数字的距离,然后选择距离最小的数字。

利润函数和距离是在此方法之外计算的,并不重要。

到目前为止我想出的是使用很多if语句。但是,我想知道是否会有更有效的方法来实现这一目标。

public void bestSolution(List<ROUTE> LS, List<ROUTE> SA, List<ROUTE> RR)
{
    int profitLS = profitRoutes(LS);
    int profitSA = profitRoutes(SA);
    int profitRR = profitRoutes(RR);

    int distanceLS = totalDistance(LS);
    int distanceSA = totalDistance(SA);
    int distanceRR = totalDistance(RR);

    if ((profitLS > profitSA) & (profitLS > profitRR))
    {

    }

}

4 个答案:

答案 0 :(得分:1)

如果在三个整数之间找到最大值 -

int mostProfit = Math.max(profitLS, Math.max(profitSA, profitRR));

考虑案例 - “选择那两个的距离,然后选择距离最小的那个”

class DistanceProfit{
   private int profit;
   private int distance; 
   public DistanceProfit(int profit, int distance){
       this.profit = profit;
       this.distance = distance;
   }
}
...
//create DistanceProfit objects add to list

Collections.sort(distenceProfitList, new Comparator<DistenceProfit>{
    public int compare(DistenceProfit dp1, DistenceProfit dp2){
         if(dp1.getProfit()==dp2.getProfit())
             return dp1.getDistance() - dp2..getDistance();
         return dp1.getProfit() - dp2.getProfit();
    }
});

答案 1 :(得分:1)

您可以使用比较结果创建TreeSet,然后选择“最佳”&#39;元素。
比较结果可能是这样的:

public class ProfitCounter implements Comparable<ProfitCounter>
{
  public ProfitCounter(List<ROUTE> route)
  {
    this.route = route;
    profit = profitRoutes(route);
    distance = totalDistance(route);
  }

  @Override
  public int compareTo(ProfitCounter other)
  {
    int result;
    result = profit - other.profit;
    if (result == 0)
      result = other.distance - distance;
    return (result);
  }

  private List<ROUTE> route;
  private int         profit;
  private int         distance;

} // class ProfitCounter

答案 2 :(得分:0)

我在这些行上使用了一些东西。不限制你使用3个参数。

public class RouteCalc implements Comparable<RouteCalc> {

    private final List routes;

    public RouteCalc(List routes) {
        this.routes = routes;
    }

    public static int calcProfit(List routes) {

        //use the list to calculate profit
        return 0;
    }

    public static int calcDistance(List routes) {

        //use the list to calculate distance
        return 0;
    }

    @Override
    public int compareTo(@NonNull RouteCalc another) {

        final int profitA = calcProfit(this.routes);
        final int profitB = calcProfit(another.routes);
        //swap parameters to change from ascending to descending and vice-versa
        final int compare = Integer.compare(profitA, profitB);

        //if same profit, compare distance
        if (compare == 0) {

            final int distanceA = calcDistance(this.routes);
            final int distanceB = calcDistance(another.routes);
            return Integer.compare(distanceA, distanceB);
        } else
            return compare;
    }

    //sample usage
    public static void main(String args[]) {

        final List<RouteCalc> allRoutes = new ArrayList<>();
        //add routes
        final RouteCalc bestRoute = Collections.max(allRoutes);
    }
}

答案 3 :(得分:0)

static class CalculatedRoute {

    public static CalculatedRoute mostProfitableOf(List<CalculatedRoute> calculatedRoutes) {
        return Collections.max(calculatedRoutes, BY_PROFIT_AND_DISTANCE);
    }

    public static final Comparator<CalculatedRoute> BY_PROFIT_AND_DISTANCE = new Comparator<CalculatedRoute>() {
        @Override
        public int compare(CalculatedRoute o1, CalculatedRoute o2) {
            int cmp = o2.profit - o1.profit;
            if (cmp == 0) {
                cmp = o1.distance - o2.distance;
            }
            return cmp;
        }
    };

    private final List<ROUTE> routeList;
    private final int profit;
    private final int distance;

    public CalculatedRoute(List<ROUTE> routeList, int profit, int distance) {
        this.profit = profit;
        this.distance = distance;
    }
    public List<ROUTE> getRouteList() {
        return routeList;
    }
    public int getProfit() {
        return profit;
    }
    public int getDistance() {
        return distance;
    }
}

public List<ROUTE> mostProfitableOf(List<ROUTE> LS, List<ROUTE> SA, List<ROUTE> RR) {
    return CalculatedRoute.mostProfitableOf(Arrays.asList(
            new CalculatedRoute(LS, profitRoutes(LS), totalDistance(LS)),
            new CalculatedRoute(SA, profitRoutes(SA), totalDistance(SA)),
            new CalculatedRoute(RR, profitRoutes(RR), totalDistance(RR))
    )).getRouteList();
}