Dev.baelanche

[백준 2909] 캔디 구매 본문

Data Structure & Algorithm/PS - JAVA

[백준 2909] 캔디 구매

baelanche 2019. 4. 12. 21:24
반응형

 

 

주어진 사탕 가격을 k 자리에서 반올림하여 출력한다.

 

1. 값의 범위를 고려하여 자료형은 double 로 했다.

2. 주어진 가격을 10의 배수로 나누어 소수점을 만들어 반올림 후 다시 10의 배수를 곱했다.

3. 위 방법대로 하면 10의 배수는 10^k 이다.

 

 

public class Main {
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        double c = sc.nextLong();
        int k = sc.nextInt();
        long pow = (long)Math.pow(10, k);
        c /= pow;
        c = Math.round(c);
        c *= pow;
        
        System.out.println((long)c);
        sc.close();
    }
}
반응형

'Data Structure & Algorithm > PS - JAVA' 카테고리의 다른 글

[백준 10824] 네 수  (0) 2019.04.13
[백준 2033] 반올림  (0) 2019.04.12
[백준 1260] DFS와 BFS  (0) 2019.04.12
[백준 10409] 서버  (0) 2019.04.12
[백준 2804] 크로스워드 만들기  (0) 2019.04.12
Comments