gpt4 book ai didi

python - 两个英制长度之差

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:17:57 25 4
gpt4 key购买 nike

我有一个程序,它采用两个英制长度(英里、码、英尺和英寸)并输出它们的和与差。我的问题是,当差异为负时,输出不正确。

def to_inches(miles, yards, feet, inches):
return inches + (feet * 12) + (yards * 36) + (miles * 63360)


def from_inches(inches):
miles = inches // 63360
inches %= 63360

yards = inches // 36
inches %= 36

feet = inches // 12
inches %= 12

return (miles, yards, feet, inches)

units1 = [int(n) for n in input().split(' ')]
units2 = [int(n) for n in input().split(' ')]

m_sum = from_inches(to_inches(*units1) + to_inches(*units2))
m_diff = from_inches(to_inches(*units1) - to_inches(*units2))

print(' '.join(str(n) for n in m_sum))
print(' '.join(str(n) for n in m_diff))

输入:

2 850 1 7
1 930 1 4

输出:

4 21 0 11
0 1680 1 3

输入:

0 1 0 0
1 0 0 0

预期输出:

1 1 0 0
-0 1759 0 0

实际输出:

1 1 0 0
-1 1 0 0

最佳答案

这是因为当涉及到负数时,Python 对模运算符 % 的处理有点奇怪。查看此答案:https://stackoverflow.com/a/3883019/436282

关于python - 两个英制长度之差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24686370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com