2016-05-25 5 views
0

Ich arbeite gerade an einem Code, der die beste Lösung finden muss. Zuerst überprüfe ich, ob einer der drei größer ist als die anderen beiden. Daher gibt es ein Maximum, das nur einmal auftritt. Wenn es zwei Zahlen gibt, die größer sind als die dritte, aber einander gleich sind, muss ich den Abstand dieser beiden vergleichen und dann wird derjenige mit der kleinsten Entfernung gewählt.Das Maximum zwischen drei ganzen Zahlen finden und wissen, welches gewählt wird

Die Gewinnfunktionen und Entfernungen werden außerhalb dieser Methode berechnet und nicht so wichtig.

Was ich bis jetzt gefunden habe, ist es, eine Menge if-Anweisungen zu verwenden. Ich habe mich jedoch gefragt, ob es eine effizientere Methode dafür geben würde.

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)) 
    { 

    } 

} 
+0

ich alle in ein Array Ihr Wert Bevorratung würde vorschlagen, und dann für jedes Element des Arrays es zu den anderen vergleichen:
Das Vergleichsergebnis könnte etwas Ähnliches sein. –

Antwort

1

Bei max zwischen drei ganze Zahlen finden -

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

Fall Betrachtet man - "Abstand der beiden letztgenannten und dann derjenige mit dem geringsten Abstand gewählt wird"

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

_Wenn es zwei Zahlen gibt, die größer als die dritte, aber einander gleich sind, muss ich den Abstand dieser beiden vergleichen und dann wird derjenige mit der kleinsten Entfernung gewählt. Ihre Lösung ignoriert diesen Teil. –

+0

ok, lass mich meine Ans mit Usecase aktualisieren. –

0

Id etwas in diesen Zeilen verwenden. Begrenzt nicht auf 3 Parameter.

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); 
    } 
} 
1

Sie könnten einen TreeSet mit den Vergleichsergebnissen und wählen Sie das ‚größte‘ Element erstellen.

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 
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(); 
} 
+0

können Sie Collections.max anstatt das ganze Ding zu sortieren – Dexter

+0

@Dexter das ist wahr –