第五楼被吞了 重发一下
然后我又修改了一下逻辑,改成如果下次要投的钱超过原本金额的一半,那么也终止,重新变成1块
function [finalMoney, moneyMatrix] = gambleMoneyUpdated(m, n)
% 初始化
currentMoney = m; % 当前剩余的钱
bet = 1; % 当前轮的投入
moneyMatrix = zeros(1, n); % 记录每轮后剩余的钱
% 对每一轮进行模拟
for i = 1:n
if currentMoney <= 0 % 没钱了就停止
break;
end
if rand() <= 0.5 % 一半的概率赢
currentMoney = currentMoney + bet; % 赢了就加上投入的钱
bet = 1; % 下一轮重新投入1块钱
else % 一半的概率输
currentMoney = currentMoney - bet; % 输了就减去投入的钱
if bet > currentMoney / 2 % 如果下次需要投入的钱超过剩余钱的1/2
bet = 1; % 下一轮也从1开始投入
else
bet = min(2 * bet, currentMoney); % 否则,下一轮翻倍投入,但不超过剩余的钱
end
end
moneyMatrix(i) = currentMoney; % 记录这一轮后剩余的钱
end
% 绘图
figure;
plot(1:n, moneyMatrix);
title('Money Variation Over Rounds');
xlabel('Round');
ylabel('Money Left');
% 返回最终的剩余金额和每轮的金额记录
finalMoney = currentMoney;
end