본문 바로가기

Algorithm/Algospot

[algospot]Ratio

문제

 

https://algospot.com/judge/problem/read/RATIO

 

게임의 총 게임횟수와 승리수가 주어질때 현재의 승률보다 1% 승률을 올리기 위해서 필요한 승리 수를 구하는 문제이다.

현재승률은 값을 구했을 때 1.34343434345555% 라면 내림으로해서 1%인것으로 한다.

 

 

입력

게임횟수  N은 1<=N<=1000000000 이고 승리횟수 M은 0<=M<=N이 된다.

 

출력

승률을 올리는데 필요한 최소한의 연승수.

 

풀이

일단 먼저 수식을 통해서 위의 내용을 풀어본다.

 

 

위의 식과같이 풀어볼수가 있는데 마지막에 나온 x의 최소값이 승리숫자가된다. 주의할 사항은 현재 승률이 99%일때이다.

99%일때 는 승을 추가하더라도 100%가 될수없기때문에 -1을 출력해줘야한다. 결과값도 infinity로 출력되기때문에 예외처리를 해줘야한다.

 

 

 

 

 

 

 

package com.tutorial;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RATIO {
    public static void main(String[] args)throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System. in));
        int cnt = Integer.parseInt(br.readLine());
        while (cnt -- > 0) {
            String[] str = br.readLine().split(" ");
            double total = Double.parseDouble(str[0]);
            double win = Double.parseDouble(str[1]);
            if (total >= 1 && total <= 1000000000 && win <= total && win >= 0) {
                double ratio = Math.floor(((double)win * 100 l) / total) + 1;
                double result = (ratio * total - 100 l * win) / (100 l - ratio);
                if (Double.isInfinite(result)) 
                    System.out.printf("-1\n");
                 else if (result > 0) 
                    System.out.printf("%.0f\n", Math.ceil(result));
                 else 
                    System.out.printf("-1\n");
                
            }
        }
    }
}

 

'Algorithm > Algospot' 카테고리의 다른 글

[algospot]DRAWRECT  (0) 2014.12.15
[algospot]FIXPAREN  (0) 2014.12.15
[algospot]BRACKETS2  (0) 2014.12.15
[algospot]HAMMINGCODE  (0) 2014.12.15
[algospot]WEIRD  (0) 2014.12.15