- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试编写一些代码来解决这个数学问题:有 8 支球队,他们都会互相踢足球比赛(所以总共 7+6+...+1 = 28 场比赛)并且他们有结果只有两种机会:赢或输。每支球队至少有 1 胜 1 负的组合有多少种。如果您无法回答问题,请说出来,让我再次尝试解释。问题是我的代码打印无限增加的数字(这是一个无限循环,我在函数中编写了 print 语句来了解问题是什么)这是我的代码,您认为问题是什么?谢谢。
num = 8
team = [0,0,0,0,0,0,0,0]
order = []
how_many_stats = 0
temp = 0
for i in range(num):
for t in range(i+1 , num):
order.append([i,t])
total_matches = len(order) - 1
curr_matches = - 1
def do_match():
global num
global team
global order
global how_many_stats
global temp
global total_matches
global curr_matches
print(how_many_stats)
curr_matches += 1
team[order[curr_matches][0]] += 1 # first wins
if not curr_matches == total_matches:
do_match()
else:
for i in range(num):
if team[i] > 0 and team[i] < 7: #7/8?
temp += 1
if temp == num:
how_many_stats += 1
temp = 0
team[order[curr_matches][0]] -= 1 # take back the action
team[order[curr_matches][1]] += 1 # second wins
if not curr_matches == total_matches:
do_match()
else:
for i in range(num):
if team[i] > 0 and team[i] < 7: #7/8?
temp += 1
if temp == num:
how_many_stats += 1
temp = 0
team[order[curr_matches][1]] -= 1
curr_matches -= 1
return
do_match()
print(how_many_stats)
解释:我为比赛声明了一条道路,并将它们放入一个数组中(第一队对第二队,第一队对第三队等),然后我按照该数组的顺序将它们付诸行动。如果这条路满足或不满足我们的条件,可能会为每场比赛建立一棵关于输赢的树,并在每个分支的末端建立一个控制结构。如果满足,则将how_many_stats的值增加1,并后退一步尝试另一条路,以此类推,如果不满足,则再次后退1步查找其他路。如果已经查看了下面的两个节点,请再次后退,依此类推。
最佳答案
在第 21 行添加一些调试逻辑后:
print("called do_match with num: %d, team: %s, order: %s, how_many_stats: %d, temp: %d, total_matchs: %d, curr_matches: %d" % (
num, str(team), str(order), how_many_stats, temp, total_matches, curr_matches
))
并让它运行一会儿:
called do_match with num: 8, team: [7, 4, 4, 1, 4, 1, 2, 4], order: [[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7], [4, 5], [4, 6], [4, 7], [5, 6], [5, 7], [6, 7]], how_many_stats: 0, temp: 0, total_matchs: 27, curr_matches: 26
called do_match with num: 8, team: [7, 4, 4, 1, 4, 0, 3, 3], order: [[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7], [4, 5], [4, 6], [4, 7], [5, 6], [5, 7], [6, 7]], how_many_stats: 0, temp: 0, total_matchs: 27, curr_matches: 25
...
called do_match with num: 8, team: [7, 4, 4, 1, 3, 1, 3, 4], order: [[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7], [4, 5], [4, 6], [4, 7], [5, 6], [5, 7], [6, 7]], how_many_stats: 0, temp: 0, total_matchs: 27, curr_matches: 26
called do_match with num: 8, team: [7, 4, 4, 1, 3, 0, 4, 3], order: [[0, 1], [0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7], [4, 5], [4, 6], [4, 7], [5, 6], [5, 7], [6, 7]], how_many_stats: 0, temp: 0, total_matchs: 27, curr_matches: 25
我得出的结论是,你的代码确实终止了,你的问题是你的匹配树中有太多的可能性。
例如,通过将团队数量减少到 4 并再次运行,它确实会终止:
called do_match with num: 4, team: [0, 0, 0, 0], order: [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]], how_many_stats: 0, temp: 0, total_matchs: 5, curr_matches: -1
....
called do_match with num: 4, team: [0, 1, 2, 2], order: [[0, 1], [0, 2], [0, 3], [1, 2], [1, 3], [2, 3]], how_many_stats: 32, temp: 0, total_matchs: 5, curr_matches: 4
32
因此,您需要找出一种更好的算法,能够计算匹配数,而无需暴力破解匹配树并计算符合您要求的结果
at least 1 win and 1 lose
例如,您可以计算每支球队恰好赢一场或输一场的可能性数量,然后从可能结果的总数中减去该结果(只需确保不要将它们计入两次,或者可以独立发生)
target=total_number-exactly_one_win-exactly_one_loss+exactly_one_win_and_one_loss
关于python - python 递归函数不停地转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43452290/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!