华为OD机试真题 - 最大股票收益
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] prices = in.nextLine().split(" ");
int[] pricesTemp = changeToRMB(prices);
System.out.println(getResult(pricesTemp));
}
public static int getResult(int[] prices) {
int sum = 0;
int price = 0;
for (int i = 0; i < prices.length; i++) {
if (isPeak(prices, i) && price != 0)
sum += prices[i] - price;
if (isValley(prices, i))
price = prices[i];
}
return sum;
}
public static int[] changeToRMB(String[] prices) {
int[] pricesTemp = new int[prices.length];
for (int i = 0; i < prices.length; i++) {
if (prices[i].endsWith("Y"))
pricesTemp[i] = Integer.parseInt(prices[i].substring(0, prices[i].length() - 1));
else
pricesTemp[i] = Integer.parseInt(prices[i].substring(0, prices[i].length() - 1)) * 7;
}
return pricesTemp;
}
public static boolean isPeak(int[] prices, int i) {
if (i == 0 && prices[i + 1] < prices[i])
return true;
else if (i == 0)
return false;
if (i == prices.length - 1 && prices[i - 1] < prices[i])
return true;
else if (i == prices.length - 1)
return false;
if (prices[i - 1] < prices[i] && prices[i + 1] < prices[i])
return true;
return false;
}
public static boolean isValley(int[] prices, int i) {
if (i == 0 && prices[i + 1] > prices[i])
return true;
else if (i == 0)
return false;
if (i == prices.length - 1 && prices[i - 1] > prices[i])
return true;
else if (i == prices.length - 1)
return false;
if (prices[i - 1] > prices[i] && prices[i + 1] > prices[i])
return true;
return false;
}
Scanner in = new Scanner(System.in);
String[] prices = in.nextLine().split(" ");
int[] pricesTemp = changeToRMB(prices);
System.out.println(getResult(pricesTemp));
}
public static int getResult(int[] prices) {
int sum = 0;
int price = 0;
for (int i = 0; i < prices.length; i++) {
if (isPeak(prices, i) && price != 0)
sum += prices[i] - price;
if (isValley(prices, i))
price = prices[i];
}
return sum;
}
public static int[] changeToRMB(String[] prices) {
int[] pricesTemp = new int[prices.length];
for (int i = 0; i < prices.length; i++) {
if (prices[i].endsWith("Y"))
pricesTemp[i] = Integer.parseInt(prices[i].substring(0, prices[i].length() - 1));
else
pricesTemp[i] = Integer.parseInt(prices[i].substring(0, prices[i].length() - 1)) * 7;
}
return pricesTemp;
}
public static boolean isPeak(int[] prices, int i) {
if (i == 0 && prices[i + 1] < prices[i])
return true;
else if (i == 0)
return false;
if (i == prices.length - 1 && prices[i - 1] < prices[i])
return true;
else if (i == prices.length - 1)
return false;
if (prices[i - 1] < prices[i] && prices[i + 1] < prices[i])
return true;
return false;
}
public static boolean isValley(int[] prices, int i) {
if (i == 0 && prices[i + 1] > prices[i])
return true;
else if (i == 0)
return false;
if (i == prices.length - 1 && prices[i - 1] > prices[i])
return true;
else if (i == prices.length - 1)
return false;
if (prices[i - 1] > prices[i] && prices[i + 1] > prices[i])
return true;
return false;
}
全部评论
相关推荐
04-03 17:47
北京中南海业余大学 Java AI牛可乐:哇,听起来你很激动呢!杭州灵枢维度科技听起来很厉害呀~你逃课去白马培训,老冯会同意吗?不过既然你这么感兴趣,肯定是有原因的吧!
对了,想了解更多关于这家公司或者求职相关的问题吗?可以点击我的头像私信我哦,我可以帮你更详细地分析一下!
点赞 评论 收藏
分享
