我正在执行Google foo.bar挑战,并制作了此python代码来回答它。问题是当输入数字超过16位时,答案总是返回不可能。在我看来这很不正确,因为在测试数字时,我发现某些输入数字和重复次数中存在重复,其中加另一个数字将始终返回另一个位值数字。
例如:
输入3和94将返回33
输入3和994将返回333
输入3和99999999994将返回33333333333
输入3和99999999999999994将返回“不可能”
我不明白为什么一定数量的地方中的任何内容在我的代码中都会返回“不可能”。
此代码将最大为10 ^ 50的两个随机数字作为字符串输入,表示为M和F。使用这些数字,您必须找到达到这两个数字所需的最少周期数。每个循环执行F功能或M功能。一个F函数等于currentFnum + currentMnum,一个M函数等于currentMnum + currentFnum。
例如,如果您有3 M和2 F,则可以得到5 M和2 F或3 M和5 F。
def answer(M, F):
count = 0
currentM = int(M)
currentF = int(F)
listCycleType = []
def getGreater(currentF, currentM):
if currentM > currentF:
greater = [currentM, "M"]
return greater
elif currentM == currentF:
greater = [currentM, "M"]
return greater
else:
greater = [currentF, "F"]
return greater
def getLesser(currentF, currentM):
if currentM < currentF:
lesser = [currentM, "M"]
return lesser
elif currentM == currentF:
lesser = [currentM, "M"]
return lesser
else:
lesser = [currentF, "F"]
return lesser
greater = getGreater(currentF, currentM)
lesser = getLesser(currentF, currentM)
for x in range(1000000):
greater = getGreater(currentF, currentM)
lesser = getLesser(currentF, currentM)
if currentF > 1 and currentM > 1 and currentF == currentM:
return "impossible"
if currentM < 1 or currentF < 1:
return "impossible"
if currentM == 1:
count = count + currentF - 1
return str(count)
elif currentF == 1:
count = count + currentM - 1
return str(count)
yes = float(greater[0]) / lesser[0]
if not yes.is_integer():
yes = greater[0] / lesser[0]
count = count + yes
if greater[1] == "M":
currentM = currentM - yes * currentF
continue
else:
currentF = currentF - yes * currentM
continue
if greater[1] == "M":
currentM = currentM - currentF
count = count + 1
elif greater[1] == "F":
currentF = currentF - currentM
count = count + 1
else:
currentM = currentM - currentF
count = count + 1
if currentM == 1 and currentF == 1:
return str(count)
return "impossible"
print answer("3", "99999999999999994")
答案 0 :(得分:0)
如果您更改
,如果不是,则为is.in_integer():
到
如果更大[0]%小于[0]!= 0:
则可以正常工作。