슈콩

[BOJ] 백준 1735 분자 합 본문

Algorithms/Baekjoon

[BOJ] 백준 1735 분자 합

shukong 2025. 9. 10. 23:45

[문제]

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

 

 

[소스 코드]

import java.io.*;
import java.util.*;
public class Main {
    public static void main(String[] args) throws IOException {
    	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer st = new StringTokenizer(br.readLine());
    	int a1 = Integer.parseInt(st.nextToken());
    	int b1 = Integer.parseInt(st.nextToken());
    	st = new StringTokenizer(br.readLine());
    	int a2 = Integer.parseInt(st.nextToken());
    	int b2 = Integer.parseInt(st.nextToken());
    	int a = a1*b2 + a2*b1;
    	int b = b1*b2;
    	int d = gcd(a,b);
    	System.out.println(a/d+" "+b/d);
    }
    private static int gcd(int a,int b) {
    	while(a!=0) {
    		int temp = a;
    		a = b%a;
    		b = temp;
    	}
    	return b;
    }
}