漫画:程序教你玩转股票





































public class StockProfit {public static int maxProfitFor1Time(int prices[]) {if(prices==null || prices.length==0) {return 0;}int minPrice = prices[0];int maxProfit = 0;for (int i = 1; i < prices.length; i++) {if (prices[i] < minPrice) {minPrice = prices[i];} else if(prices[i] - minPrice > maxProfit){maxProfit = prices[i] - minPrice;}}return maxProfit;}public static void main(String[] args) {int[] prices = {9,2,7,4,3,1,8,4};System.out.println(maxProfitFor1Time(prices));}}







public int maxProfitForAnyTime(int[] prices) {int maxProfit = 0;for (int i = 1; i < prices.length; i++) {if (prices[i] > prices[i-1])maxProfit += prices[i] - prices[i-1];}return maxProfit;}







更多精彩推荐
点分享 点点赞 点在看
关注公众号:拾黑(shiheibook)了解更多
[广告]赞助链接:
四季很好,只要有你,文娱排行榜:https://www.yaopaiming.com/
让资讯触达的更精准有趣:https://www.0xu.cn/
关注网络尖刀微信公众号随时掌握互联网精彩
赞助链接
排名
热点
搜索指数
- 1 习近平将发表二〇二六年新年贺词 7904141
- 2 2026年国补政策来了 7808738
- 3 东部战区:开火!开火!全部命中! 7712893
- 4 2026年这些民生政策将惠及百姓 7616985
- 5 小学食堂米线过期2.5小时被罚5万 7519709
- 6 解放军喊话驱离台军 原声曝光 7428214
- 7 为博流量直播踩烈士陵墓?绝不姑息 7327605
- 8 每月最高800元!多地发放养老消费券 7238391
- 9 数字人民币升级 1月1日起将计付利息 7141831
- 10 2026年1月1日起 一批新规将施行 7040675











程序人生
