백준: 1735(분수 합)

강지안·2023년 6월 8일
0

baekjoon

목록 보기
55/186

문제

코드

import java.io.*;
import java.util.Scanner;

public class q1735 {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);

        int[][] inputs = new int[2][2];

        inputs[0][0] = sc.nextInt();
        inputs[0][1] = sc.nextInt();
        inputs[1][0] = sc.nextInt();
        inputs[1][1] = sc.nextInt();

        int denominator = inputs[0][1] * inputs[1][1] / getGcd(inputs[0][1], inputs[1][1]);
        int numerator = inputs[0][0] * (denominator / inputs[0][1]) + inputs[1][0] * (denominator / inputs[1][1]);

        int gcd = getGcd(numerator, denominator);
        System.out.print(numerator/gcd + " " + denominator/gcd);
    }
    public static int getGcd(int a, int b) {
        if(b == 0) return a;
        else return getGcd(b, a%b);
    }
}

주의

매번 문제를 중간중간 잊어 조건을 빠뜨려 틀리는 경우가 있다.
문제를 대충 읽지 말고 제대로 파악하는 버릇을 들이자

0개의 댓글