Java { this.route=路线; 利润=利润(路线); 距离=总距离(路线); } @凌驾 公共整数比较(ProfitCounter其他) { int结果; 结果=利润-其他.利润; 如果(结果==0) 结果=其他距离-距离; 返回(结果); } 私有列表
{ this.route=路线; 利润=利润(路线); 距离=总距离(路线); } @凌驾 公共整数比较(ProfitCounter其他) { int结果; 结果=利润-其他.利润; 如果(结果==0) 结果=其他距离-距离; 返回(结果); } 私有列表路由; 私利; 私有整数距离; }//类ProfitCounterJava { this.route=路线; 利润=利润(路线); 距离=总距离(路线); } @凌驾 公共整数比较(ProfitCounter其他) { int结果; 结果=利润-其他.利润; 如果(结果==0) 结果=其他距离-距离; 返回(结果); } 私有列表,java,max,Java,Max,{ this.route=路线; 利润=利润(路线); 距离=总距离(路线); } @凌驾 公共整数比较(ProfitCounter其他) { int结果; 结果=利润-其他.利润; 如果(结果==0) 结果=其他距离-距离; 返回(结果); } 私有列表路由; 私利; 私有整数距离; }//类ProfitCounter 静态类计算路由{ 公共静态CalculatedRoute mostProfitableOf(列表calculatedRoutes){ return Collections.ma
静态类计算路由{
公共静态CalculatedRoute mostProfitableOf(列表calculatedRoutes){
return Collections.max(按利润和距离计算的路线);
}
公共静态最终比较器按利润和距离=新比较器(){
@凌驾
公共整数比较(计算路由o1,计算路由o2){
int cmp=o2.利润-o1.利润;
如果(cmp==0){
cmp=o1.距离-o2.距离;
}
返回cmp;
}
};
私人最终名单;
私人最终利润;
私有最终整数距离;
公共计算路线(列出路线列表、整数利润、整数距离){
这个。利润=利润;
这个距离=距离;
}
公共列表getRouteList(){
返回路线图;
}
公共int getProfit(){
利润回报;
}
公共int getDistance(){
返回距离;
}
}
公共列表mostProfitableOf(列表LS、列表SA、列表RR){
返回CalculatedRoute.mostProfitableOf(Arrays.asList(
新的计算路线(LS、利润率(LS)、总距离(LS)),
新计算路线(SA、利润率(SA)、总距离(SA)),
新的计算路线(RR、利润率(RR)、总距离(RR))
)).getRouteList();
}
静态类计算路由{
公共静态CalculatedRoute mostProfitableOf(列表calculatedRoutes){
return Collections.max(按利润和距离计算的路线);
}
公共静态最终比较器按利润和距离=新比较器(){
@凌驾
公共整数比较(计算路由o1,计算路由o2){
int cmp=o2.利润-o1.利润;
如果(cmp==0){
cmp=o1.距离-o2.距离;
}
返回cmp;
}
};
私人最终名单;
私人最终利润;
私有最终整数距离;
公共计算路线(列出路线列表、整数利润、整数距离){
这个。利润=利润;
这个距离=距离;
}
公共列表getRouteList(){
返回路线图;
}
公共int getProfit(){
利润回报;
}
公共int getDistance(){
返回距离;
}
}
公共列表mostProfitableOf(列表LS、列表SA、列表RR){
返回CalculatedRoute.mostProfitableOf(Arrays.asList(
新的计算路线(LS、利润率(LS)、总距离(LS)),
新计算路线(SA、利润率(SA)、总距离(SA)),
新的计算路线(RR、利润率(RR)、总距离(RR))
)).getRouteList();
}
我建议将所有值存储在一个数组中,然后将数组中的每个元素与其他元素进行比较。我建议将所有值存储在一个数组中,然后将数组中的每个元素与其他元素进行比较。如果有两个数大于第三个数,但彼此相等,我必须比较这两个的距离,然后选择距离最小的一个。你的解决方案忽略了这一部分。如果有两个数字大于第三个,但彼此相等,我将不得不比较这两个数字的距离,然后选择距离最小的一个。您的解决方案确实忽略了这一部分。您可以使用Collections.max而不是对整个内容进行排序您可以使用Collections.max而不是对整个内容进行排序
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))
{
}
}
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();
}
});
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);
}
}
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
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();
}