'1753'에 해당되는 글 1건

  1. 2021.05.31 [ Python ] 백준 1735 파이썬
파이썬2021. 5. 31. 00:28
728x90

[ 문제 ]

https://www.acmicpc.net/problem/1735

 

1735번: 분수 합

첫째 줄과 둘째 줄에, 각 분수의 분자와 분모를 뜻하는 두 개의 자연수가 순서대로 주어진다. 입력되는 네 자연수는 모두 30,000 이하이다.

www.acmicpc.net

 

 

[ 코드 ]

 

import sys
input=sys.stdin.readline

a1,a2=map(int,input().split()) #a1/a2 분수
b1,b2=map(int,input().split())

#최소공배수 구하기
def lcm(x,y):
    for i in range(x,(x*y)+1): # x ~ x*y
        if(i%x==0 and i%y==0):
            return i
#최대공약수 구하기
def gcd(x,y):
    #y가 0될 때 까지
    while y:
        x,y  = y, x%y
    return x

#분모 denominator
denom = lcm(a2,b2)

#분자 numerator
num = a1*(denom/a2) + b1*(denom/b2)

#분자분모의 최대공약수로 나눌거임
div = gcd(num,denom)
denom /= div
num /= div

print(int(num),int(denom))

 

 

[ 결과 ]

반응형

'파이썬' 카테고리의 다른 글

[ Python ] 백준 13300 파이썬  (0) 2021.05.31
[ Python ] 백준 10828 파이썬  (0) 2021.05.31
[ Python ] 백준 1475 파이썬  (0) 2021.05.31
[ Python ] 백준 1453 파이썬  (0) 2021.05.31
[ Python ] 문자열 replace로 swap 하기  (0) 2021.05.29
Posted by mminky