我正在尝试创建一个应用程序,它将计算异国情调的parimutuel下注成本。我已经为某些类型的下注找到了几个,但从来没有一个可以解决单个下注类型的所有情况。如果我能找到一个算法,可以计算所有可能的组合,我就可以用这个公式来解决我的其他问题。
附加信息:我需要计算数组的排列。例如:
组1= 1,2,3
组2= 2,3,4
组3= 3,4,5
这三组数字的所有可能的排列是什么?每个排列从每组中取一个数字。每个排列没有重复,这意味着一个数字不能出现在1个以上的位置。所以2,4,3是有效的,但2,4,4是无效的。
谢谢你的帮助。
发布于 2009-07-22 19:33:56
像大多数有趣的问题一样,你的问题有几个解决方案。我写的算法(下面)是我想到的最简单的东西。
我发现最容易将问题想成树搜索:第一个组,即根,它包含的每个数字都有一个子组,其中每个子组是第二个组。第二个组对于它包含的每个数字都有一个第三组子组,第三个组对于它包含的每个数字都有一个第四组子组,依此类推。您所要做的就是查找从根到叶子的所有有效路径。
然而,对于许多拥有大量数字的群体来说,如果没有任何启发式方法,这种方法将被证明是缓慢的。你可以做的一件事是按照组的大小对组列表进行排序,最小的组优先。这将是一种快速失败的方法,通常情况下,它会发现置换不是最早有效的。前瞻、arc-consistency和回溯是您可能想要考虑的其他事情。很抱歉,我只能包含一个链接,因为这是我的第一篇文章,但你可以在维基百科上找到这些东西。
## Algorithm written in Python ##
## CodePad.org has a Python interpreter
Group1 = [1,2,3] ## Within itself, each group must be composed of unique numbers
Group2 = [2,3,4]
Group3 = [3,4,5]
Groups = [Group1,Group2,Group3] ## Must contain at least one Group
Permutations = [] ## List of valid permutations
def getPermutations(group, permSoFar, nextGroupIndex):
for num in group:
nextPermSoFar = list(permSoFar) ## Make a copy of the permSoFar list
## Only proceed if num isn't a repeat in nextPermSoFar
if nextPermSoFar.count(num) == 0:
nextPermSoFar.append(num) ## Add num to this copy of nextPermSoFar
if nextGroupIndex != len(Groups): ## Call next group if there is one...
getPermutations(Groups[nextGroupIndex], nextPermSoFar, nextGroupIndex + 1)
else: ## ...or add the valid permutation to the list of permutations
Permutations.append(nextPermSoFar)
## Call getPermutations with:
## * the first group from the list of Groups
## * an empty list
## * the index of the second group
getPermutations(Groups[0], [], 1)
## print results of getPermutations
print 'There are', len(Permutations), 'valid permutations:'
print Permutations发布于 2011-01-24 13:22:45
几年后修订:-
一段时间后,我登录了我的SE帐户,注意到了这个问题,并意识到我写的东西甚至没有回答你:
下面是一些python代码
import itertools
def explode(value, unique):
legs = [ leg.split(',') for leg in value.split('/') ]
if unique:
return [ tuple(ea) for ea in itertools.product(*legs) if len(ea) == len(set(ea)) ]
else:
return [ tuple(ea) for ea in itertools.product(*legs) ]调用explode的基础是,每个分支由/分隔,每个位置由
对于你的三连胜计算,你可以用下面的公式来计算:
result = explode('1,2,3/2,3,4/3,4,5', True)
stake = 2.0
cost = stake * len(result)
print cost对于一个超级棒来说
result = explode('1,2,3/2,4,5/1,3,6,9/2,3,7,9', True)
stake = 2.0
cost = stake * len(result)
print cost对于pick4 (设置为False)
result = explode('1,2,3/2,4,5/3,9/2,3,4', False)
stake = 2.0
cost = stake * len(result)
print cost希望这能有所帮助
发布于 2014-07-24 21:58:45
这是我所知道的三连胜的最简单的通用公式。
第一个选择的数量;第二个选择的B=number;第三个选择的C=number;第一个和第二个选择的AB=number;AC=no。对于第一个和第三个;BC=no。第二个和第三个;和ABC=the no.所有第一,第二和第三的选择。公式为(AxBxC)-(ABxC)-(ACxB)-(BCxA)+(2xABC)
因此,对于您的示例::
Group 1 = 1,2,3
Group 2 = 2,3,4
Group 3 = 3,4,5解决方案是::(3x3x3)-(2x3)-(1x3)-(2x3)+(2x1)=14。希望有一个我不知道的更简单的方法。现在有没有人知道First4的通用公式?
https://stackoverflow.com/questions/1112195
复制相似问题