-
백준 1735 - 분수 합BOJ-Algorithm 2022. 1. 9. 02:30
https://www.acmicpc.net/problem/1735
1735번: 분수 합
첫째 줄과 둘째 줄에, 각 분수의 분자와 분모를 뜻하는 두 개의 자연수가 순서대로 주어진다. 입력되는 네 자연수는 모두 30,000 이하이다.
www.acmicpc.net
first_son, first_mother = map(int, input().split()) second_son, second_mother = map(int, input().split()) def GCD(x,y): while(y): x,y = y,x%y return x def LCM(x,y): result = (x*y)//GCD(x,y) return result ans_mother = LCM(first_mother, second_mother) ans_son = first_son*ans_mother/first_mother+second_son*ans_mother/second_mother div = GCD(ans_son, ans_mother) ans_son /= div ans_mother /= div print(f'{int(ans_son)} {int(ans_mother)}')
기약분수가 아닌 둘 사이의 분모는 최소공배수가 될 것이고,
기약분수 형태가 아닌 분자는 최소공배수에서 자기 분모 값을 나눈 값을 분자에 더한 것의 합이 될 것이다.
이 때의 형태는 기약분수가 아니므로, 분모와 분자의 최대공약수를 찾아 분모와 분자에 각각 나눠 준 뒤 해당 값을 분자, 분모 순으로 출력하면 된다.
'BOJ-Algorithm' 카테고리의 다른 글
백준 1978 - 소수 찾기 (0) 2022.01.10 백준 1251 - 단어나누기 (0) 2022.01.10 백준 2609 - 최대공약수와 최소공배수 (0) 2022.01.09 백준 1436 - 영화감독 숌 (0) 2022.01.09 백준 1764 - 듣보잡 (0) 2022.01.09