[BOJ][C#] 1072 게임

LimJaeJun·2023년 12월 11일
0

PS/BOJ

목록 보기
59/108

📕 문제

📌 링크

📗 접근 방식

📘 코드

namespace BOJ_1072
{
    class Program
    {
        static void Main()
        {
            using StreamReader sr = new StreamReader(new BufferedStream(Console.OpenStandardInput()));
            using StreamWriter sw = new StreamWriter(new BufferedStream(Console.OpenStandardOutput()));

            var inputs = sr.ReadLine().Split().Select(long.Parse).ToArray();

            long x = inputs[0];
            long y = inputs[1];

            int probability = GetProbability(x,y);

            if (IsNeverChange(probability))
            {
                sw.Write("-1");
            }
            else
            {
                int answer = BinarySearch(x, y, probability);
                
                sw.Write(answer);
            }
            
            sw.Flush(); sw.Close(); sr.Close();
        }

        static int BinarySearch(long x, long y, int curProbability)
        {
            long start = 0;
            long end = 1_000_000_000;

            while (start <= end)
            {
                long mid = (start + end) >> 1;
                long newProbability = ((y + mid) * 100) / (x + mid);

                if (curProbability >= newProbability)
                {
                    start = mid + 1;
                }
                else
                {
                    end = mid - 1;
                }
            }

            return (int)start;
        }

        static int GetProbability(long x, long y) => (int)(y * 100 / x);
        static bool IsNeverChange(int probability) => probability > 98;
    }
}

📙 오답노트

  1. 이분탐색 함수에서 newProbability 계산시 y가 1,000,000,000일 경우 오버플로우가 나기 때문에 long타입으로 수정하였지만 틀림
  2. 이분탐색 함수뿐 아니라 입력값도 오버플로우가 날 수 있기 때문에 long타입으로 선언해주었다.

📒 알고리즘 분류

  • 수학
  • 이분 탐색
profile
Dreams Come True

0개의 댓글

관련 채용 정보