728x90
https://www.acmicpc.net/problem/1735
1735번: 분수 합
첫째 줄과 둘째 줄에, 각 분수의 분자와 분모를 뜻하는 두 개의 자연수가 순서대로 주어진다. 입력되는 네 자연수는 모두 30,000 이하이다.
www.acmicpc.net
## 분수 합
import sys
input = sys.stdin.readline
def gcd(a, b):
while b > 0:
a, b = b, a % b
return a
a, b = map(int, input().split())
c, d = map(int, input().split())
g = gcd(b, d)
## 분모(b 와 d의 최소공배수)
denominator = b * d // g
## 분자
numerator = a * (d // g) + c * (b // g)
## 기약 분수로 만든 후 출력(분자와 분모의 최대공약수를 각각 나눠줌)
temp = gcd(denominator, numerator)
print(numerator // temp, denominator // temp)
MEMO
!! 분자, 분모를 코드로 처음 다룬 날 !!
728x90
728x90
'백준[baekjoon] > Python' 카테고리의 다른 글
백준(baekjoon) [Python] - 4134번: 다음 소수 (0) | 2023.03.12 |
---|---|
백준(baekjoon) [Python] - 2485번: 가로수 (0) | 2023.03.12 |
백준(baekjoon) [Python] - 13241번: 최소공배수 (0) | 2023.03.10 |
백준(baekjoon) [Python] - 11729번: 하노이 탑 이동 순서 (0) | 2023.03.10 |
백준(baekjoon) [Python] - 2750번: 수 정렬하기 (0) | 2023.03.06 |