华为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;
}
全部评论
相关推荐
程序员小白条:别去小厂,初创或者外包,尽量去中小,100-499和500-999,专门做互联网产品的,有公司自研的平台和封装的工具等等,去学习一些业务相关的,比如抽奖,积分兑换,SSO认证,风控,零售等等,目标 Java 后端开发吗?你要不考虑直接走大厂测开?如果技术不行的话,有面试你也很难过的
点赞 评论 收藏
分享