코테/백준

[백준/JAVA] 5585번: 거스름돈

imname1am 2023. 7. 7. 01:13
반응형

🔺 문제

 

5585번: 거스름돈

타로는 자주 JOI잡화점에서 물건을 산다. JOI잡화점에는 잔돈으로 500엔, 100엔, 50엔, 10엔, 5엔, 1엔이 충분히 있고, 언제나 거스름돈 개수가 가장 적게 잔돈을 준다. 타로가 JOI잡화점에서 물건을 사

www.acmicpc.net

 

 

🔺 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.*;
import java.io.*;
 
public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int price = 1000 - Integer.parseInt(br.readLine());
        long cnt = 0;
        
        while(price > 0) {
            int tmp = 0;
            
            if(price / 500 > 0) {
                tmp = price/500;
                cnt += tmp;
                price -= tmp * 500;
            }
            else if(price / 100 > 0) {
                tmp = price/100;
                cnt += tmp;
                price -= tmp * 100;                
            }
            else if(price / 50 > 0) {
                tmp = price/50;
                cnt += tmp;
                price -= tmp * 50;                
            }
            else if(price / 10 > 0) {
                tmp = price/10;
                cnt += tmp;
                price -= tmp * 10;                
            }
            else if(price / 5 > 0) {
                tmp = price/5;
                cnt += tmp;
                price -= tmp * 5;                
            }
            else if(price / 1 > 0) {
                tmp = price/1;
                cnt += tmp;
                price -= tmp * 1;                
            }
        }
 
        System.out.println(cnt);
    }
}
 
cs
✅ 해결 아이디어
✔ 그리디
- 큰 단위의 잔돈 순서로 배열을 저장해 남은 돈에서 나눠줌.

 

 


🔺 다른 풀이들

- 개쩐다.. 이렇게 푸는 것이었구만.ㅋㅋㅋㅋㅋ 머쓱하네....

 

[백준 - 5585번] 거스름돈 - JAVA 정리 및 해설

안녕하세요. 오늘 풀어볼 문제는 거스름돈입니다. 이 문제는 그리디 방식의 문제입니다. https://www.acmicpc.net/problem/5585 5585번: 거스름돈 문제 타로는 자주 JOI잡화점에서 물건을 산다. JOI잡화점에는

sundries-in-myidea.tistory.com

 

로그인

 

www.acmicpc.net

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.*;
 
class Main {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
        int n = 1000 - scan.nextInt();
        int[] arr = {500,100,50,10,5,1};
        int cnt = 0;
 
        for(int i = 0 ; i < arr.length ; i++){
           cnt += n / arr[i];
           n %= arr[i];
        }    
 
        System.out.println(cnt);
  }
}
 
cs

💬 느낀 점

효율적 방법을 생각해내자,,,ㅋㅋㅋㅋㅋ

너무 머쓱함...

 

 

1회독 2회독 3회독 4회독 5회독
V        
반응형