码迷,mamicode.com
首页 > 其他好文 > 详细

题目2:买卖股票的最佳时机

时间:2017-03-07 23:04:13      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:void   out   size   names   int   时机   space   股票   std   

#include "stdafx.h"
#include<iostream>
using namespace std;
#include<vector>


int maxProfit(vector<int> & prices)
{


int n = prices.size();
if (n <= 1)
return 0;
int max =0;
for (int i = 1,min=prices[0];i < n ;i++) {
int Dvalue=prices[i] - min;
if ( Dvalue > max)
max = Dvalue;
if (prices[i] < min)
min = prices[i];
}
return max;

}

void main()
{
vector<int>prices;
int B[]={3,2,3,1,2,12 };
for(int i=0;i<6;i++)
{
prices.push_back(B[i]);
}

int p=maxProfit(prices);
cout<<p;

}

题目2:买卖股票的最佳时机

标签:void   out   size   names   int   时机   space   股票   std   

原文地址:http://www.cnblogs.com/123zzq/p/6517150.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!