본문 바로가기
Problem Solving/Baekjoon

[백준] 18512 점프 점프 - Brute Force / Java

by graycode 2023. 9. 1.

 문제 링크

 

18512번: 점프 점프

첫째 줄에 두 사람이 한 번에 멀리뛰기를 하는 거리 X, Y와 시작 지점의 위치 값 P1, P2가 각각 공백을 기준으로 구분되어 자연수로 주어진다. (1 ≤ X, Y, P1, P2 ≤ 100)

www.acmicpc.net

 

 풀이 코드

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

        int x = read(), y = read(), a = read(), b = read();

        int n = -1, i = 0;
        while (i++ < 198) {
            if (a == b) {
                n = a;
                break;
            }

            if (a < b) a += x;
            else b += y;
        }

        bw.write(String.valueOf(n));
        bw.flush();
    }

    private static int read() throws IOException {
        int c, n = System.in.read() & 15;
        while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);

        return n;
    }

}

댓글