Kattis问题帮助——密码锁(错误的答案)



密码锁卡蒂斯

我一直在解决这个问题,我只是不能简单地找到我的代码出了什么问题。为了解决这个问题,系统会提示用户在打开组合键锁时找出总的旋转度。用户输入很简单:四个数字给起始位置,然后锁解决方案的三个数字。

我发现解决这个问题最简单的方法是将每个数字转换成角度:1是9度,40是360度(总共有40个凹槽)。我还将0转换为40以创建一个数轴1 ->这样做似乎更容易些。

我已经用我当前的解决方案尝试了各种结束情况-甚至在其他可接受的在线解决方案中引用了我的代码。我似乎找不到破坏我代码的测试用例。

这是我得到的(它是用Python 3写的-我是初学者):

def main():
userInput = [int(z) for z in input().split()]
for i in range(len(userInput)):
if userInput[i] == 0:
userInput[i] = 40
postiton = userInput[0]
one = userInput[1]
two = userInput[2]
three = userInput[3]
total = 720
if postiton == one:
total += 0
elif postiton > one:
total += (postiton - one) * 9
else:
total += (40 - (one - postiton)) * 9
total += 360
if two == one:
total += 0
elif two > one:
total += (two - one) * 9
else:
total += (40 - (one - two)) * 9
if two == three:
total += 0
elif two > three:
total += (two - three) * 9
else:
total += (40 - (three - two)) * 9
print(total)

if __name__ == "__main__":
main()

不太确定从这里开始-任何关于解决这类问题的帮助或建议都会很有帮助。

我读了你的评论,实际上有问题产生正确的程度值。我用你的代码做了一些调整。看看这个练习的链接,我改进了代码,再次考虑从0到39的密码锁值。在审查你的代码时,我认为绊倒你的两件事是如何在逆时针和顺时针旋转时比较一个位置和下一个位置,以及如何处理相对于顺时针和逆时针旋转锁的净位置比较。

在此基础上,我提供了以下代码片段,其中包含一堆注释和打印语句,以跟踪拨号盘的进度。

def main():
print('Enter four distinct numbers: ')
userInput = [int(z) for z in input().split()]

print('User entered: ', userInput)
position = userInput[0]
one = userInput[1]
two = userInput[2]
three = userInput[3]   

# Start with two full turns clockwise.

total = 720

print('Initial two turns: ', total)

# Turn clockwise until at the first number
if (position == one):
total+=0
elif(position > one):
total += ((position - one) * 9)     # Just a short distance to go clockwise
else:
total += ((40 + position - one) * 9)    # Have to go to 0, then on to position one

print('Position one: ', total)
# Turn counterclockwise one full turn
total += 360

print('Counterclockwise one turn: ', total)
if (two == one):
total+=0
elif (two < one):           # Two is less than one - need to go around back to zero and then to two
total += (40 - one + two) * 9
else:
total += ((two - one) * 9)  # Short distance to go counterclockwise

print('Position two: ', total)
if (two == three):
total+=0
elif (two > three):         # Two is greater than three - short distance to go clockwise
total+= (three - two) * 9
else:
total+=(40 + two - three) * 9   # Continue to position 0, then on to the position #3
print('After final position: ',total)

if __name__ == "__main__" :
main()

我用一组简单的值(0,20,10,30)进行了测试,当我与手动计算学位总数相比时,一切似乎都加起来了。不管怎样,试一试吧。

致意。

最新更新