from random import *
seed(1) #随机数种子,确保结果可复现
p=5 #售价
c=2 #进价
s=1 #残值
for i in [5,20,50,100,200,500,1000,2000,5000,10000]: #样本数量
D=[]
F=0
for n in range(i):
D.append(randint(1000,2000)) #随机抽取需求量
print(f"样本量为{i}时需求为{D[:20]}.")
for q in range(min(D),max(D)): #最优解一定在最低需求与最高需求之间产生
E=0
for d in D:
E+=p*min(d,q)+s*max(q-d,0)-c*q
if E < F:
print(f"最优解:订货量q为{q-1},平均收益E为{F/len(D)}")
break
else:
F=E
print("{:=^50s}".format("Split Line"))