强化一件装备,有10%的几率成功,如果未成功,则下次成功几率增加10%(即:第一次未成功,则第二次成功几率为20%),求装备强化成功的期望次数?
function f(deep, p)
if p >= 0.99 then -- lua精度问题
return deep
else
print("f("..deep..","..p..") = "..deep.."*"..p.." + "..(1-p).." * f("..(deep+1)..","..(p+0.1)..")")
return deep*p + (1-p)*f(deep+1,p+0.1)
end
end
print(f(1, 0.1))
-- f(1, 0.1) = 1*0.1 + (1-0.1) * f(2, 0.2)
ans = 1*0.1 + 0.9 * (
2*0.2 + 0.8 * (
3*0.3 + 0.7 * (
4*0.4 + 0.6 * (
5*0.5 + 0.5 * (
6*0.6 + 0.4 * (
7*0.7 + 0.3 * (
8*0.8 + 0.2 * (
9*0.9 + 0.1 * (
10*1)))))))))
print(ans)
-- f(1,0.1) = 1*0.1 + 0.9 * f(2,0.2)
-- f(2,0.2) = 2*0.2 + 0.8 * f(3,0.3)
-- f(3,0.3) = 3*0.3 + 0.7 * f(4,0.4)
-- f(4,0.4) = 4*0.4 + 0.6 * f(5,0.5)
-- f(5,0.5) = 5*0.5 + 0.5 * f(6,0.6)
-- f(6,0.6) = 6*0.6 + 0.4 * f(7,0.7)
-- f(7,0.7) = 7*0.7 + 0.3 * f(8,0.8)
-- f(8,0.8) = 8*0.8 + 0.2 * f(9,0.9)
-- f(9,0.9) = 9*0.9 + 0.1 * f(10,1)
-- f(10,1) = 10*1
略作一般化
某游戏抽卡出货概率是2%,如果连续50次没有出货,接下来每次单抽出货概率+2%,直到出货为止。
即如果一直不出货:
第51次概率为4%
第52次概率为6%
第53次概率为8%
求抽卡出货的期望次数
local add_p = 0.02
function f(deep, p)
if p >= 0.9999 then -- 精度问题
return deep
else
if deep<=50 then
print("f("..deep..","..p..")\t= "..deep.."*"..p.." + "..(1-p).." * f("..(deep+1)..","..(p)..")")
return deep*p + (1-p)*f(deep+1,p)
else
print("f("..deep..","..p..")\t= "..deep.."*"..p.." + "..(1-p).." * f("..(deep+1)..","..(p+add_p)..")")
return deep*p + (1-p)*f(deep+1,p+add_p)
end
end
end
local base_p = 0.02
print(f(1, base_p))
--[[
f(1,0.02) = 1*0.02 + 0.98 * f(2,0.02)
f(2,0.02) = 2*0.02 + 0.98 * f(3,0.02)
f(3,0.02) = 3*0.02 + 0.98 * f(4,0.02)
f(4,0.02) = 4*0.02 + 0.98 * f(5,0.02)
f(5,0.02) = 5*0.02 + 0.98 * f(6,0.02)
f(6,0.02) = 6*0.02 + 0.98 * f(7,0.02)
f(7,0.02) = 7*0.02 + 0.98 * f(8,0.02)
f(8,0.02) = 8*0.02 + 0.98 * f(9,0.02)
f(9,0.02) = 9*0.02 + 0.98 * f(10,0.02)
f(10,0.02) = 10*0.02 + 0.98 * f(11,0.02)
f(11,0.02) = 11*0.02 + 0.98 * f(12,0.02)
f(12,0.02) = 12*0.02 + 0.98 * f(13,0.02)
f(13,0.02) = 13*0.02 + 0.98 * f(14,0.02)
f(14,0.02) = 14*0.02 + 0.98 * f(15,0.02)
f(15,0.02) = 15*0.02 + 0.98 * f(16,0.02)
f(16,0.02) = 16*0.02 + 0.98 * f(17,0.02)
f(17,0.02) = 17*0.02 + 0.98 * f(18,0.02)
f(18,0.02) = 18*0.02 + 0.98 * f(19,0.02)
f(19,0.02) = 19*0.02 + 0.98 * f(20,0.02)
f(20,0.02) = 20*0.02 + 0.98 * f(21,0.02)
f(21,0.02) = 21*0.02 + 0.98 * f(22,0.02)
f(22,0.02) = 22*0.02 + 0.98 * f(23,0.02)
f(23,0.02) = 23*0.02 + 0.98 * f(24,0.02)
f(24,0.02) = 24*0.02 + 0.98 * f(25,0.02)
f(25,0.02) = 25*0.02 + 0.98 * f(26,0.02)
f(26,0.02) = 26*0.02 + 0.98 * f(27,0.02)
f(27,0.02) = 27*0.02 + 0.98 * f(28,0.02)
f(28,0.02) = 28*0.02 + 0.98 * f(29,0.02)
f(29,0.02) = 29*0.02 + 0.98 * f(30,0.02)
f(30,0.02) = 30*0.02 + 0.98 * f(31,0.02)
f(31,0.02) = 31*0.02 + 0.98 * f(32,0.02)
f(32,0.02) = 32*0.02 + 0.98 * f(33,0.02)
f(33,0.02) = 33*0.02 + 0.98 * f(34,0.02)
f(34,0.02) = 34*0.02 + 0.98 * f(35,0.02)
f(35,0.02) = 35*0.02 + 0.98 * f(36,0.02)
f(36,0.02) = 36*0.02 + 0.98 * f(37,0.02)
f(37,0.02) = 37*0.02 + 0.98 * f(38,0.02)
f(38,0.02) = 38*0.02 + 0.98 * f(39,0.02)
f(39,0.02) = 39*0.02 + 0.98 * f(40,0.02)
f(40,0.02) = 40*0.02 + 0.98 * f(41,0.02)
f(41,0.02) = 41*0.02 + 0.98 * f(42,0.02)
f(42,0.02) = 42*0.02 + 0.98 * f(43,0.02)
f(43,0.02) = 43*0.02 + 0.98 * f(44,0.02)
f(44,0.02) = 44*0.02 + 0.98 * f(45,0.02)
f(45,0.02) = 45*0.02 + 0.98 * f(46,0.02)
f(46,0.02) = 46*0.02 + 0.98 * f(47,0.02)
f(47,0.02) = 47*0.02 + 0.98 * f(48,0.02)
f(48,0.02) = 48*0.02 + 0.98 * f(49,0.02)
f(49,0.02) = 49*0.02 + 0.98 * f(50,0.02)
f(50,0.02) = 50*0.02 + 0.98 * f(51,0.02)
f(51,0.02) = 51*0.02 + 0.98 * f(52,0.04)
f(52,0.04) = 52*0.04 + 0.96 * f(53,0.06)
f(53,0.06) = 53*0.06 + 0.94 * f(54,0.08)
f(54,0.08) = 54*0.08 + 0.92 * f(55,0.1)
f(55,0.1) = 55*0.1 + 0.9 * f(56,0.12)
f(56,0.12) = 56*0.12 + 0.88 * f(57,0.14)
f(57,0.14) = 57*0.14 + 0.86 * f(58,0.16)
f(58,0.16) = 58*0.16 + 0.84 * f(59,0.18)
f(59,0.18) = 59*0.18 + 0.82 * f(60,0.2)
f(60,0.2) = 60*0.2 + 0.8 * f(61,0.22)
f(61,0.22) = 61*0.22 + 0.78 * f(62,0.24)
f(62,0.24) = 62*0.24 + 0.76 * f(63,0.26)
f(63,0.26) = 63*0.26 + 0.74 * f(64,0.28)
f(64,0.28) = 64*0.28 + 0.72 * f(65,0.3)
f(65,0.3) = 65*0.3 + 0.7 * f(66,0.32)
f(66,0.32) = 66*0.32 + 0.68 * f(67,0.34)
f(67,0.34) = 67*0.34 + 0.66 * f(68,0.36)
f(68,0.36) = 68*0.36 + 0.64 * f(69,0.38)
f(69,0.38) = 69*0.38 + 0.62 * f(70,0.4)
f(70,0.4) = 70*0.4 + 0.6 * f(71,0.42)
f(71,0.42) = 71*0.42 + 0.58 * f(72,0.44)
f(72,0.44) = 72*0.44 + 0.56 * f(73,0.46)
f(73,0.46) = 73*0.46 + 0.54 * f(74,0.48)
f(74,0.48) = 74*0.48 + 0.52 * f(75,0.5)
f(75,0.5) = 75*0.5 + 0.5 * f(76,0.52)
f(76,0.52) = 76*0.52 + 0.48 * f(77,0.54)
f(77,0.54) = 77*0.54 + 0.46 * f(78,0.56)
f(78,0.56) = 78*0.56 + 0.44 * f(79,0.58)
f(79,0.58) = 79*0.58 + 0.42 * f(80,0.6)
f(80,0.6) = 80*0.6 + 0.4 * f(81,0.62)
f(81,0.62) = 81*0.62 + 0.38 * f(82,0.64)
f(82,0.64) = 82*0.64 + 0.36 * f(83,0.66)
f(83,0.66) = 83*0.66 + 0.34 * f(84,0.68)
f(84,0.68) = 84*0.68 + 0.32 * f(85,0.7)
f(85,0.7) = 85*0.7 + 0.3 * f(86,0.72)
f(86,0.72) = 86*0.72 + 0.28 * f(87,0.74)
f(87,0.74) = 87*0.74 + 0.26 * f(88,0.76)
f(88,0.76) = 88*0.76 + 0.24 * f(89,0.78)
f(89,0.78) = 89*0.78 + 0.22 * f(90,0.8)
f(90,0.8) = 90*0.8 + 0.2 * f(91,0.82)
f(91,0.82) = 91*0.82 + 0.18 * f(92,0.84)
f(92,0.84) = 92*0.84 + 0.16 * f(93,0.86)
f(93,0.86) = 93*0.86 + 0.14 * f(94,0.88)
f(94,0.88) = 94*0.88 + 0.12 * f(95,0.9)
f(95,0.9) = 95*0.9 + 0.1 * f(96,0.92)
f(96,0.92) = 96*0.92 + 0.08 * f(97,0.94)
f(97,0.94) = 97*0.94 + 0.059999999999999 * f(98,0.96)
f(98,0.96) = 98*0.96 + 0.039999999999999 * f(99,0.98)
f(99,0.98) = 99*0.98 + 0.019999999999999 * f(100,1)
]]
如果规则再改改,前50次费用固定600,对于第50次后每次抽卡,如果一直不出货,除了概率加2%,费用减6,求出货一次的期望费用。
解法敬请期待…