프로그래밍 문제

[백준][C#] 1072번, 게임

sorry0101 2024. 9. 12. 00:06

문제

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

형택이가 이상하다...

풀이

승률이 1퍼라도 오르면 되니까 1차 방정식으로 해결했다.

ex. 100 80

X = 100, Y = 80, Z = 80(%)

(이겨야하는 횟수) => N

 

공식

renew = (Z + 1) * 0.01;

(X + N) * renew = Y + N

∴ N = (X * renew - Y) / renew;

더보기
namespace Algorithm
{
    class Program
    {
        static int X, Y, Z; // 이긴 횟수, 이긴 게임, 승률

        static void Main(string[] args)
        {
            Answer();
        }

        static void Answer ()
        {
            int[] inputs = Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
            X = inputs[0];
            Y = inputs[1];
            
            // 게임을 한번도 안했다면 한번만 해도 바뀌니까 1
            if(X == 0) 
            {
                Console.WriteLine(1);
                return;
            }

            // 부동소수점 처리를 주의하기 위해 decimal로 풀이
            Z = Convert.ToInt32(Math.Truncate((decimal)Y / (decimal)X * 100));

            CalcRecord();
        }

        // 승률이 1퍼라도 오르면 되니까 1차 방정식으로 해결
        static void CalcRecord()
        {
            decimal newRecord = (Z + 1) * (decimal)0.01;

            // 바뀌지 않을 경우 -1
            if(newRecord > 1 || (1 - newRecord) == 0)
            {
                Console.WriteLine(-1);
                return;
            }

            var result = Math.Ceiling((X * newRecord - Y) / (1 - newRecord));
            Console.WriteLine(result);
        }
    }
}

반례

입력: 1000 999
출력: -1

입력: 593496 334550
출력: 8704

입력: 6904434 3986253
출력: 43616

Comment


- 직접 써보면서 하니 더 낫다.
- (-1) 처리를 주의해야 했다.



https://github.com/SolHaan/algorithm_study

 

GitHub - SolHaan/algorithm_study: 1 day 1 algorithm

1 day 1 algorithm. Contribute to SolHaan/algorithm_study development by creating an account on GitHub.

github.com

참고