我试图写一些代码,输出所有可能的变化,坑圈策略的比赛。假设比赛有6圈长,在此期间进行了3次停站,可能的组合是: 111123 111223 112223 122223 122233等等.其中,1,2和3只是用来可视化时,停站已经做了。我一直在寻找一个数学函数,它输出可能的总变化数,但没有用,有人知道吗?
此外,是否有一种方法可以创建一个数组,该数组包含所有不同的组合,显示组合中有多少个1、2和3s?
谢谢
发布于 2022-03-18 15:38:26
这似乎是一个关于组合学和在数组中对结果进行编码的问题。从编程(MATLAB)的角度来看,“有多少个组合”的答案是nchoosek(nLaps,nPitstops)。简单地说,由于排序是不相关的,也就是说,无论何时进行,停站都是相同的,所以计算问题的答案是使用二项式温度计解决的。如果你有6圈,其中3圈将包含一个停站,那么你会得到"6选择3“可能的组合。
对于问题的数组部分,MATLAB已经处理了确定所有组合的“艰苦”工作,允许我们输入一个向量来生成组合。解决这一问题的办法如下:
% initialize
nLaps = 6;
nPitstops = 3;
laps = 1:nLaps;
pits = 1:nPitstops;
% calculate binomial coefficient
b = nchoosek(nLaps,nPitstops);
% generate combinations
C = nchoosek(laps,nPitstops);
% create solution matrix
result = ones(b,nLaps);
% loop through each combination
for B = 1:b
% loop through the pit identifiers
for P = pits
% determine the start lap
start = C(B,P);
% determine the next start lap...
if P < pits(end)
stop = C(B,P+1);
else
% ... or simply repeat until end
stop = nLaps;
end
% store pit identifier in these lap positions
result(B,start:stop) = P;
end
end上述守则将产生:
result =
1 2 3 3 3 3
1 2 2 3 3 3
1 2 2 2 3 3
1 2 2 2 2 3
1 1 2 3 3 3
1 1 2 2 3 3
1 1 2 2 2 3
1 1 1 2 3 3
1 1 1 2 2 3
1 1 1 1 2 3
1 1 2 3 3 3
1 1 2 2 3 3
1 1 2 2 2 3
1 1 1 2 3 3
1 1 1 2 2 3
1 1 1 1 2 3
1 1 1 2 3 3
1 1 1 2 2 3
1 1 1 1 2 3
1 1 1 1 2 3我被你想要的符号弄糊涂了。在第一圈之后进行第一个停站的情况下,例如,112333,使用您的表示法,我希望在第一圈位置上有一个0,像这样:012333,因为还没有停站。如果这是您想要的表示法,那么您将用result函数来初始化这个zeros()变量。
https://stackoverflow.com/questions/71517339
复制相似问题