amazon 股票的最佳买入和卖出点
来源:互联网 发布:起诉淘宝店无中文标签 编辑:程序博客网 时间:2024/05/01 01:54
/* * File: stock_price.cpp * Author: hongbin * 给出一个股价序列,求出最佳的买入和卖出点, 也就是求序列后面的元素跟前面元素的最大值。 */#include <cstdlib>#include <iostream>#include <string.h>#include <stdlib.h>#include <stdio.h>using namespace std;//O(N^2)bool find_max_profit1(int stock_price[], int length, int& buy_index, int &sell_index ){ int max_profit=0; int end=length-1; int price_diff1=0; bool find=false; buy_index=sell_index=0; if(length <2) return find; max_profit=stock_price[1]-stock_price[0]; for(int index=0; index < length-1; index++ ) for (int step=index+1;step < length; step++ ) { price_diff1=stock_price[step]-stock_price[index]; if(price_diff1>max_profit) { max_profit= price_diff1; buy_index=index; sell_index=step; find=true; } } return find; }//O(N)bool find_max_profit2(int stock_price[], int length, int& buy_index, int &sell_index ){ int max_profit=0; int end=length-1; int price_diff1=0,price_diff2=0; bool find=false; buy_index=sell_index=0; if(length <2 ) return find; max_profit=stock_price[1]-stock_price[0]; for(int index=0; index<end; index++ ) { price_diff1=stock_price[index+1] -stock_price[index]; price_diff2=stock_price[index+1] -stock_price[buy_index]; if (price_diff2 > max_profit) { sell_index = index+1; max_profit=price_diff2; find=true; } if(price_diff1 >max_profit) { buy_index=index; sell_index = index+1; max_profit=price_diff1; find=true; } } return find; }/* * */int main(int argc, char** argv) { int price[]={2,5,3,4,1,25,1,8,9}; int buy_index=0; int sell_index=0; find_max_profit2(price,9,buy_index,sell_index); cout<<"find_max_profit_good find: buy index:"<<buy_index+1<<" sell index:"<<sell_index+1<<endl; cout<<"profit is: "<<(price[sell_index]-price[buy_index])<<endl; buy_index=sell_index=0; //use another algorithm to do again find_max_profit1(price,9,buy_index,sell_index); cout<<"find_max_profit1: : buy index:"<<buy_index+1<<" sell index:"<<sell_index+1<<endl; cout<<"profit is: "<<(price[sell_index]-price[buy_index])<<endl; return 0;}/* */
0 0
- amazon 股票的最佳买入和卖出点
- 股票的最佳买入卖出时间
- 股票买入卖出手续费
- LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)
- 假如一个数组存储了一个股票,在一天交易窗口内各时间点的股票价格(正整数),只允许一次买入和一次卖出,请提供一个算法,计算出通过买入和卖出可以得到的最大利润
- 假如一个数组存储了一个股票,在一天交易窗口内各时间点的股票价格(正整数),只允许一次买入和一次卖出,请提供一个算法,计算出通过买入和卖出可以得到的最大利润
- Best Time to Buy and Sell Stock 最佳时间买入卖出股票(一次买入卖出) @LeetCode
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)
- 网易游戏面试--两次股票买入卖出的最大收益
- 最佳时间买入卖出股票 Best Time to Buy and Sell Stock LeetCode
- 给出N天的股价,能买入和卖出股票,但是任何时候手上最多持有一份股票.允许交易K次求最多的收益.
- 卖出股票的几点技巧
- leetCode:121\122\309 股票买入和卖出获取最大收益问题
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- Best Time to Buy and Sell Stock III 最佳时间买入卖出股票(最多两次买卖)@LeetCode
- 股市买入卖出时间点选择问题
- 股市买入卖出时间点选择问题
- Java PathFinder(一) Java PathFinder基于Eclipse的安装配置及使用
- 一种为使用Log4j的分布式应用提供云端日志服务的方法
- STL学习之路(2)
- Servlet的过滤器,监听器
- LeetCode No341. Flatten Nested List Iterator
- amazon 股票的最佳买入和卖出点
- 面向过程与面向对象的区别
- JavaScript常用内置对象及其相应属性和方法(二)
- Spark的RDD简单操作
- Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'
- Win7下硬盘安装Linux双系统
- Android开发利器
- 16_JSP Cookies 处理
- 【ZJOI2008】树的统计