这是我寻找可以从股票中获得最大利润的解决方案。
int [] aktiePris = new int [] {10,7,5,8,11,9};是一个数组,其中的索引是股市开盘后的分钟,而值是股票的价格。
例如,aktiePris [60] = 300表示在股市开盘一小时后股票的价值为300。
现在,我的代码返回我可以通过买卖一只股票赚取的最大利润。我希望能够持有不止一只股票。我如何找到所有可能的利润并打印出来?
import java.text.SimpleDateFormat;
import java.util.Calendar;
public class ProfitCalculator {
static int minValue, maxValue, maxDiff;
static Calendar timeMin, timeMax;
static int indeksMinMinut, indeksMaxMinut;
public static void main(String args[]) {
int[] aktiePris = new int[]{10, 7, 5, 8, 11, 9};
int profit = findProfit(aktiePris);
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
timeMin = findTime();
timeMin.add(timeMin.MINUTE, indeksMinMinut);
timeMax = findTime();
timeMax.add(timeMax.MINUTE, indeksMaxMinut);
System.out.println("Best time & price for buying is " + timeFormat.format(timeMin.getTime()) + " for " + minValue + " EUR." + "\n"
+ "Best time & price for selling is " + timeFormat.format(timeMax.getTime()) + " for " + maxValue + " EUR." + "\n"
+ "Profit: " + profit);
}
public static int findProfit(int[] inputArray) {
if (inputArray.length < 1)
return 0;
maxDiff = 0;
minValue = inputArray[0];
maxValue = minValue;
for (int i = 1; i < inputArray.length; i++) {
if (inputArray[i] > maxValue) {
maxValue = inputArray[i];
indeksMaxMinut = i;
int priceDiff = maxValue - minValue;
if (priceDiff > maxDiff) {
maxDiff = priceDiff;
}
} else if (inputArray[i] < minValue) {
minValue = maxValue = inputArray[i];
indeksMinMinut = i;
}
}
return maxDiff;
}
public static Calendar findTime() {
Calendar calendar = Calendar.getInstance();
calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 30);
return calendar;
}
}
答案 0 :(得分:0)
创建一个共享分析器类,该类封装了所需的功能:
class ShareAnalyzer {
private final int[] aktiePris;
private int sellMinut = 0, buyMinut = 0, buyPrice, sellPrice;
ShareAnalyzer(int[] aktiePris) {
this.aktiePris = aktiePris;
findProfit();
}
private void findProfit() {
if (aktiePris.length < 1) return ;
int minValue = aktiePris[0];
int maxValue = minValue;
int indeksMaxMinut = 0, indeksMinMinut = 0;
for (int i = 1; i < aktiePris.length; i++) {
if (aktiePris[i] > maxValue) {
maxValue = aktiePris[i];
indeksMaxMinut = i;
int priceDiff = maxValue - minValue;
if (priceDiff > getProfit()) {
sellPrice = maxValue;
buyPrice = minValue;
sellMinut = indeksMaxMinut;
buyMinut = indeksMinMinut;
}
} else if (aktiePris[i] < minValue) {
minValue = maxValue = aktiePris[i];
indeksMinMinut = i;
}
}
}
//time in minutes from opening
int getSellTime() {return sellMinut;}
int getSellPrice() {return sellPrice;}
//time in minutes from opening
int getBuyTime() {return buyMinut; }
int getBuyPrice() {return buyPrice;}
int getProfit() { return sellPrice - buyPrice; }
}
您还可以添加一种方法来打印出共享分析器信息:
//this could be implemented as `toString` of ShareAnalyzer
private static void printShareInfo(ShareAnalyzer shareAnalyzer){
System.out.println("Best time & price for buying is " + shareAnalyzer.getBuyTime() + " minutes after opening for " + shareAnalyzer.getBuyPrice() + " EUR." + "\n"
+ "Best time & price for selling is " + shareAnalyzer.getSellTime() + " minutes after opening for " + shareAnalyzer.getSellPrice() + " EUR." + "\n"
+ "Profit: " + shareAnalyzer.getProfit());
}
为要分析的每个共享构造一个新的ShareAnalyzer
实例。
public static void main(String args[]) {
int[] aktiePris1 = new int[]{10, 7, 5, 8, 11, 9};
ShareAnalyzer shareAnalyzer1 = new ShareAnalyzer(aktiePris1);
printShareInfo(shareAnalyzer1);
int[] aktiePris2 = new int[]{2, 12, 4, 7 , 11, 9, 1 , 8};
ShareAnalyzer shareAnalyzer2 = new ShareAnalyzer(aktiePris2);
printShareInfo(shareAnalyzer2);
}
您可以运行代码并使用this link
对其进行测试