3

두 개의 제국 길이 (마일, 야드, 피트 및 인치)를 사용하는 프로그램이 있고 그 합과 차이를 출력합니다. 내 문제는 차이가 음수 일 때 출력이 올바르지 않다는 것입니다.2 개의 제국 길이의 차이

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 

예상 출력 : 01,235

16,
1 1 0 0 
-0 1759 0 0 

실제 출력은 : 그것은 음수에 올 때 파이썬이 모드 운영자 % 조금 이상한 처리하기 때문에

1 1 0 0 
-1 1 0 0 

답변

2

이 있습니다. 이 답변을 확인하십시오 : https://stackoverflow.com/a/3883019/436282

+0

분명히, 정수 나누기 연산자 ('/') 마찬가지입니다. 그래서'int (math.fmod (x, y))'와'int (x/y)'를 사용하여 끝내게되었습니다. 또한, 음수 인 경우 첫 번째 숫자 만 음수로 만들기위한 검사를 추가했습니다. – nyuszika7h