public class HorsePrice {
public static void main(String[] args) {
int[] prices = {345, 336, 389, 270, 226, 350};
trade(prices);
}
public static void trade(int[] prices) {
//总天数
int len = prices.length;
//买入的价格索引(即天数-1)
int buyIndex = 0;
//卖出的价格索引(即天数-1)
int sellIndex = 0;
//利润
int profit = 0;
for (int i = 0; i < len; ++i) {
for (int k = i; k < len; ++k) {
int p = prices[k] - prices[i];
if (p > profit) {
profit = p;
buyIndex = i;
sellIndex = k;
}
}
}
System.err.printf("第 %d 天买入,第 %d 天卖出,盈利 %d", ++buyIndex, ++sellIndex, profit);
}
}
第 5 天买入,第 6 天卖出,盈利 124