이 델타 추적하는 데 고려를, 그래서 당신은 그들이 있었다 선택적으로 일부 작업을 수행 할 수 있습니다 얼마나 알고 나중에 추가 조작. 또한 주문 및 포장 기간에 이미 Java가 제공하는 것을 사용하십시오.
예를 들어
: 주문은 원래 배열에 중요한 경우
Best amount of profit is
Profit : 5, delta=4
Profit : 10, delta=3
Profit : 1, delta=1
Profit : 6, delta=1
Profit : 7, delta=1
Profit : 6, delta=0
Profit : 7, delta=0
Profit : 10, delta=0
, 원본 유지됩니다 다른 클래스에 sortedStocks
을 포장 할 수 있습니다 :
import java.util.*;
class ProfitDelta implements Comparable<ProfitDelta> {
private int delta;
private int profit;
public ProfitDelta(int delta, int profit) { this.delta = delta; this.profit = profit; }
public int getDelta() { return delta; }
public int getProfit() { return profit; }
public int compareTo(ProfitDelta o) {
return o.delta - this.delta;
}
}
class Main {
static Integer[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
public static void main(String[] args) {
System.out.println("Best amount of profit is ");
for (ProfitDelta profit : getBestProfitAmount(stocks)) {
System.out.println("Profit : " + profit.getProfit() + ", delta=" + profit.getDelta());
}
}
public static ProfitDelta[] getBestProfitAmount(Integer[] stocks) {
ProfitDelta[] profits = new ProfitDelta[stocks.length];
List<Integer> sortedStocks = Arrays.asList(stocks);
List<ProfitDelta> sortedProfits = Arrays.asList(profits);
Collections.sort(sortedStocks);
int delta = 0;
int stockValue;
for (int i = 0; i < stocks.length; i++) {
stockValue = sortedStocks.get(i);
delta = stockValue - delta;
sortedProfits.set(i, new ProfitDelta(delta, stockValue));
delta = stockValue;
}
Collections.sort(sortedProfits);
return profits;
}
}
는 당신에게 출력을 줄 것이다 인덱스 값을 입력하고 originalIndex
을 ProfitDelta
에 추가하십시오.또는이 같은 이벤트 단순한는 :
import java.util.*;
class ProfitDelta implements Comparable<ProfitDelta> {
private int originalIndex = 0;
private int delta = 0;
private int profit;
public ProfitDelta(int index, int profit) { this.originalIndex = index; this.profit = profit; }
public int getOriginalIndex() { return originalIndex; }
public int getDelta() { return delta; }
public void setDelta(int delta) { this.delta = delta; }
public int getProfit() { return profit; }
public int compareTo(ProfitDelta o) { return o.delta - this.delta; }
}
class ProfitComparator implements Comparator<ProfitDelta> {
@Override public int compare(ProfitDelta o1, ProfitDelta o2) {
return o1.getProfit() - o2.getProfit();
}
}
class Main {
static int[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };
public static void main(String[] args) {
System.out.println("Best amount of profit is ");
for (ProfitDelta profit : getBestProfitAmount(stocks)) {
System.out.println("Profit : " + profit.getProfit() +
", delta=" + profit.getDelta() +
", originalIndex=" + profit.getOriginalIndex());
}
}
public static ProfitDelta[] getBestProfitAmount(int[] stocks) {
ProfitDelta[] profits = new ProfitDelta[stocks.length];
int delta = 0;
List<ProfitDelta> sortedProfits = Arrays.asList(profits);
for (int i = 0; i < stocks.length; i++) {
sortedProfits.set(i, new ProfitDelta(i, stocks[i]));
}
Collections.sort(sortedProfits, new ProfitComparator());
for (ProfitDelta profit : profits) {
profit.setDelta(profit.getProfit() - delta);
delta = profit.getProfit();
}
Collections.sort(sortedProfits);
return profits;
}
}
출력한다 :
Best amount of profit is
Profit : 5, delta=4, originalIndex=5
Profit : 10, delta=3, originalIndex=4
Profit : 1, delta=1, originalIndex=0
Profit : 6, delta=1, originalIndex=2
Profit : 7, delta=1, originalIndex=1
Profit : 6, delta=0, originalIndex=6
Profit : 7, delta=0, originalIndex=3
Profit : 10, delta=0, originalIndex=7
대신 당신이'[0 델타> largestDelta을 비교해야한다,로 배열 INT []'largestDelta'을 비교하고'delta'을 int로되어있다 ] – Desert
왜 1) 배열을 정렬 한 다음 마지막 요소에서 첫 번째 요소를 뺍니다. 또는 2) 최소 및 최대 요소를 추적하는 배열을 반복 한 다음 서로를 뺍니다 (약간 빨라짐)? – Patashu
이 질문의 반대쪽처럼 들립니다. http://stackoverflow.com/questions/11974208/find-the-two-most-similar-values-in-an-array-java/11974245#11974245 –