如何重写函数,使其在运行时更改作为参数输入的变量?我读过你必须在每个变量名之前写global
,但在a
、b
、c
参数之前写global不起作用,我想不出其他方法来实现它。
import sys
sys.stdin = open("/Users/isym444/Desktop/PythonCP/CP1/Codewars/Practice/input.txt", "r")
sys.stdout = open("/Users/isym444/Desktop/PythonCP/CP1/Codewars/Practice/output.txt", "w")
""" sys.stdin = open("mixmilk.in", "r")
sys.stdout = open("mixmilk.out", "w") """
c1, m1 = map(int, input().split())
c2, m2 = map(int, input().split())
c3, m3 = map(int, input().split())
def fun1(a, b, c):
amt = min(a, b - c)
a -= amt
c += amt
# pours 1 to 99
for i in range(1, 34):
fun1(m1, c2, m2)
fun1(m2, c3, m3)
fun1(m3, c1, m1)
# pour 100
fun1(m1, c2, m2)
result = [m1, m2, m3]
for i in result:
print(i)
请注意,这是USACO问题的解决方案:2018年12月比赛,铜牌问题1。混合牛奶->http://www.usaco.org/index.php?page=viewproblem2&cpid=855
我想这就是您想要的:
[...]
def fun1(a, b, c):
amt = min(a, b - c)
a -= amt
c += amt
return (a, b, c)
# pours 1 to 99
for i in range(1, 34):
m1, c2, m2 = fun1(m1, c2, m2)
m2, c3, m3 = fun1(m2, c3, m3)
m3, c1, m1 = fun1(m3, c1, m1)
# pour 100
m1, c2, m2 = fun1(m1, c2, m2)
[...]