2

可以说我正在尝试模仿一组股票价格。这些股票价格中的每一个都在一个数组中。增量是这些价格之间的差异。我正在寻找最大增量并将其返回到数组。当我尝试在 Eclipse 中实现这一点时,这两行代码出现以下错误:

if (delta > largestDelta) {
   largestDelta = stocks[i];
}

“运算符 > 未定义参数类型 int, int[]”
“类型不匹配:无法从 int 转换为 int[]”

这是我的整个代码:

public class Stocks {
    static int[] stocks = { 1, 7, 6, 7, 10, 5, 6, 10 };

    // delta = sell point - buy point
    // find largest delta

    public static int[] findMaximumProfit(int[] stocks) {

        int delta = 0;
        int[] largestDelta = {0};
        for (int i=0; i < stocks.length; i++) {
            delta = stocks[i+1] - stocks[i]; 
            if (delta > largestDelta) {
                largestDelta = stocks[i];
            }
        }
        return largestDelta;
   }
}

我可能做错了什么?

4

4 回答 4

4

改变

int[] largestDelta = {0};

int largestDelta = stocks[0];

或者

int largestDelta = 0;

还要记住更改返回类型。

于 2013-06-27T22:33:17.980 回答
0

您正在将 int,delta与 int[],进行比较largestDelta。也许,您应该简单地说int largestDelta = 0;和定义public static int findMaximumProfit(int[] stocks)?如果您正在寻找所有可能股票之间的所有增量的最大值,您需要做一个双循环:

for (int i=0; i < stocks.length; i++) {
    for (int j=i+1; j < stocks.length; j++) {
        delta = stocks[j] - stocks[i]; 
        if (delta > largestDelta) {
            largestDelta[0] = i;
            largestDelta[1] = j;    
        }
    }
}
于 2013-06-27T22:39:43.287 回答
0

考虑跟踪您的增量,这样您就可以知道它们有多少,并且以后可以选择做一些额外的操作。另外,利用 Java 在排序和包装方面已经提供的功能。

例如 :

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

会给你输出:

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到另一个将保留原始索引值的类中,并将属性添加originalIndexProfitDelta. 或者更简单的事件,像这样:

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
于 2013-06-27T23:40:09.440 回答
0

问题是您正在尝试比较一个 int 和一个数组。这当然是一个错误:delta > largestDelta.

另一个错误是您将 int 分配给数组。这一行也是一个错误:largestDelta = stocks[i];

您必须将最大 delta 更改为 int,如下所示:

int largestDelta = 0;

完成此操作后,您将能够比较deltalargestDelta,并分配stocks[i]largestDelta

希望这可以帮助!

于 2013-06-27T22:36:16.713 回答