我如何获得多份股票利润?
How do i get multiple stock profits?
这是我找到我可以从股票中获得的最大利润的解决方案。
int[] aktiePris = new int[]{10, 7, 5, 8, 11, 9};是一个数组,其中指数是股市开盘后的分钟数,值是股票的价格。
所以例如 aktiePris[60] = 300 意味着股票的价值是 300,在股市开盘后一小时。
现在我的代码 returns 我可以通过买卖一只股票获得的最大可能利润。我希望能够选择不止一只股票。我怎样才能找到所有可能的利润并打印出来?
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;
}
}
制作一个分享分析器class,它封装了你想要的功能:
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
对其进行测试
这是我找到我可以从股票中获得的最大利润的解决方案。
int[] aktiePris = new int[]{10, 7, 5, 8, 11, 9};是一个数组,其中指数是股市开盘后的分钟数,值是股票的价格。
所以例如 aktiePris[60] = 300 意味着股票的价值是 300,在股市开盘后一小时。
现在我的代码 returns 我可以通过买卖一只股票获得的最大可能利润。我希望能够选择不止一只股票。我怎样才能找到所有可能的利润并打印出来?
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;
}
}
制作一个分享分析器class,它封装了你想要的功能:
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
对其进行测试