Home 백준 설탕 배달 (2839)
Post
Cancel

백준 설탕 배달 (2839)

백준 설탕 배달 (2839)

백준 설탕배달

스크린샷 2022-01-29 오전 1 38 33

전형적인 그리디 문제이다.

대학교 갓 들어와서 c언어 배울 때 이 문제를 3일동안 머리 싸매고 풀었던 기억이 있는데

스크린샷 2022-01-29 오전 2 17 55

알고리즘을 공부하고 그리디 개념을 알고나서 보니 엄청 쉬운 문제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.io.*;  
  
public class Main {  
  
    public static void main(String[] args) throws NumberFormatException, IOException {  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        int n = Integer.parseInt(br.readLine());  
        int ans = 0;  
  
        while(n>=0) {  
            if(n % 5 ==0) {  
                ans += (n/5);  
                System.out.println(ans);  
                return;  
            }  
            n -= 3;  
            ans++;  
        }  
        System.out.println("-1");  
    }  
  
}

This post is licensed under CC BY 4.0 by the author.