본문 바로가기
Problem Solving/Baekjoon

[백준] 1072 게임 - BinarySearch / Java

by graycode 2023. 8. 8.

 문제 링크

 

1072번: 게임

김형택은 지금 몰래 Spider Solitaire(스파이더 카드놀이)를 하고 있다. 형택이는 이 게임을 이길 때도 있었지만, 질 때도 있었다. 누군가의 시선이 느껴진 형택이는 게임을 중단하고 코딩을 하기 시

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();
        long z = y * 100L / x;

        if (z < 99) {
            int low = 1, high = x;
            while (low <= high) {
                int mid = (low + high) / 2;

                if (z < 100L * (y + mid) / (x + mid)) high = mid - 1;
                else low = mid + 1;
            }

            bw.write(String.valueOf(low));
        } else bw.write("-1");

        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;
    }

}

댓글