dp练习题6 股票买卖

题目:

最近越来越多的人都投身股市,阿福也有点心动了。谨记着“股市有风险,入市需谨慎”,阿福决定先来研究一下简化版的股票买卖问题。

假设阿福已经准确预测出了某只股票在未来N天的价格,他希望买卖两次,使得获得的利润最高。为了计算简单起见,利润的计算方式为卖出的价格减去买入的价格。

同一天可以进行多次买卖。但是在第一次买入之后,必须要先卖出,然后才可以第二次买入。

现在,阿福想知道他最多可以获得多少利润。

思路:

    这里因为是2次买卖,所以我们可以求出i天前的最大收益和i天后的最大收益,然后求两个加起来的最大值,设lr为i天前的最大收益,lr2为i天后的最大收益,则可得lr[i]=max(lr[i-1],gp[i]-min1),lr2=max(lr[i-1],max1-gp[i]),其中gp代表每天股票的价格,min1代表前i天的最便宜的股票价格,max1代表最贵的股票价格。

代码:

#include<cstdio>
#include<cstring>
int ans,t,n,gp[100001],min1,lr[100001],lr2[100001];
int max(int x,int y) {return x>y?x:y;}
int min(int x,int y) {return x<y?x:y;}
int main()
{
	scanf("%d",&t);
	while (t--)
	{
		memset(lr,0,sizeof(lr));
        memset(lr2,0,sizeof(lr2));
        ans=0;
		scanf("%d",&n);
		for (int i=1;i<=n;i++)
		scanf("%d",&gp[i]);
		
		min1=gp[1];
		for (int i=1;i<=n;i++)
		{
			lr[i]=max(lr[i-1],gp[i]-min1);
			min1=min(gp[i],min1);
		}
		
		min1=gp[n];
		for (int i=n-1;i>=1;i--)
		{
			lr2[i]=max(lr2[i-1],min1-gp[i]);
			min1=max(gp[i],min1);
		}
		for (int i=1;i<=n;i++)
		  ans=max(ans,lr[i]+lr2[i]);//求加起来的最大值
		printf("%d\n",ans);
	}
}


### Python 实现买卖股票最佳时机的测试题与练习题 以下是一些关于 Python 实现买卖股票最佳时机的测试题和练习题,涵盖了不同的交易限制条件。 #### 1. 最佳买卖股票一次 编写一个函数,计算在给定的股票价格列表中,最可以获取的最大利润。假设只允许进行一次买入和一次卖出的操作。 ```python def maxProfit(prices): if not prices: return 0 min_price = prices[0] max_profit = 0 for price in prices: if price < min_price: min_price = price # 更新最低价格 elif price - min_price > max_profit: max_profit = price - min_price # 更新最大利润 return max_profit ``` 测试用例: ```python print(maxProfit([7, 1, 5, 3, 6, 4])) # 输出:5 print(maxProfit([7, 6, 4, 3, 1])) # 输出:0 ``` #### 2. 最佳买卖股票次 编写一个函数,计算在给定的股票价格列表中,最可以获取的最大利润。允许次买入和卖出的操作。 ```python def maxProfit(prices): total_profit = 0 for i in range(1, len(prices)): if prices[i] > prices[i - 1]: total_profit += prices[i] - prices[i - 1] # 累加利润 return total_profit ``` 测试用例: ```python print(maxProfit([7, 1, 5, 3, 6, 4])) # 输出:7 print(maxProfit([1, 2, 3, 4, 5])) # 输出:4 ``` #### 3. 带有冷却期的最佳买卖股票 编写一个函数,计算在给定的股票价格列表中,最可以获取的最大利润。假设每次卖出后必须等待一天(即冷却期)才能再次购买。 ```python def maxProfit(prices): if not prices: return 0 n = len(prices) dp = [[0, 0] for _ in range(n)] dp[0][0] = 0 dp[0][1] = -prices[0] for i in range(1, n): dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + prices[i]) # 不持有股票 dp[i][1] = max(dp[i - 1][1], dp[i - 2][0] - prices[i] if i > 1 else dp[i - 1][0] - prices[i]) # 持有股票 return dp[-1][0] ``` 测试用例: ```python print(maxProfit([1, 2, 3, 0, 2])) # 输出:3 ``` #### 4. 最两次交易的最佳买卖股票 编写一个函数,计算在给定的股票价格列表中,最可以获取的最大利润。假设最只能完成两笔交易。 ```python def maxProfit(prices): if not prices: return 0 n = len(prices) left_profits = [0] * n right_profits = [0] * n min_price = prices[0] for i in range(1, n): min_price = min(min_price, prices[i]) left_profits[i] = max(left_profits[i - 1], prices[i] - min_price) max_price = prices[-1] for i in range(n - 2, -1, -1): max_price = max(max_price, prices[i]) right_profits[i] = max(right_profits[i + 1], max_price - prices[i]) max_profit = 0 for i in range(n): max_profit = max(max_profit, left_profits[i] + right_profits[i]) return max_profit ``` 测试用例: ```python print(maxProfit([3, 3, 5, 0, 0, 3, 1, 4])) # 输出:6 ``` #### 5. 含有交易费用的最佳买卖股票 编写一个函数,计算在给定的股票价格列表中,最可以获取的最大利润。假设每次交易都有固定的手续费。 ```python def maxProfit(prices, fee): if not prices: return 0 cash, hold = 0, -prices[0] for price in prices[1:]: cash = max(cash, hold + price - fee) # 卖出股票 hold = max(hold, cash - price) # 买入股票 return cash ``` 测试用例: ```python print(maxProfit([1, 3, 2, 8, 4, 9], 2)) # 输出:8 ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值